Realm-js: Object is not a constructor on Typescript import * from "realm"

Created on 18 Aug 2017  路  12Comments  路  Source: realm/realm-js

Version:

  • react-native: 0.47.1
  • realm: 1.10.3

When use:

import * as Realm from "realm"

new Realm({
    schema: [{ name: 'Dog', properties: { name: 'string' } }]
})

// or
Realm.open({
    schema: [{ name: 'Dog', properties: { name: 'string' } }]
})
.then((realm) => {...})

Will show error in red screen:

Object is not a constructor (evaluating 'new Realm({
    schema: [{ name: 'Dog', properties: { name: 'string' } }]
})')

// or
Object is not a constructor (evaluating 'new _this(config)')

Change import to this type is no problem:

import Realm from "realm"

But lose type intelligence and get ts error:

Module ''realm'' has no default export.

Similar problems in stackoverflow
Cannot open Realm using typescript 2.4.1 + react native 0.46.1 because Realm is not a constructor

Thanks

O-Community T-Help

Most helpful comment

@CacaoRick could you please add a config in tsconfig.json

"allowSyntheticDefaultImports": true

All 12 comments

@CacaoRick did you try any of the tricks in https://realm.io/docs/javascript/latest/#missing-realm-constructor

Thanks @kneth
Bit I think this is not missing-realm-constructor problem, my react-native link work well.
And after I use import Realm from "realm", my APP can be executed with no error.

Here is my realm.ts code to get realm instance with right type for typescript

import Realm from "realm"
import * as RealmTypes from "realm"

const Meter: RealmTypes.ObjectSchema = {
    // ...
}

const RecordData: RealmTypes.ObjectSchema = {
    // ...
}

const Photo: RealmTypes.ObjectSchema = {
    // ...
}

const realm: RealmTypes = new Realm({ schema: [Meter, RecordData, Photo] })
export default realm

Now it can be intelligence, but still show error Module 'realm' has no default export.


@CacaoRick Thanks for clarifying. Friday I was playing with some TypeScript but did see the issue. We have to investigate a bit more.

@CacaoRick Is your issue related to #884?

I'm work in android.
But I test in iOS simulator will get same problem.
(Also just run react-native link, not configure podfile and run pod install)

@CacaoRick could you please add a config in tsconfig.json

"allowSyntheticDefaultImports": true

With "allowSyntheticDefaultImports": true in tsconfig.json

import * as Realm from "realm"

App will show red screen and display error: Object is not constructor

Change import to:

import Realm from "realm"

App work fine, but vscode will not intelligence and tslint show error

Module "realm" has no default export.

Nothing change.

What version of your Realm types ?

"@types/realm": "^1.0.3",

Works fine for me.

"realm": "^1.10.3",
"@types/realm": "^1.0.3",

Sorry my allowSyntheticDefaultImports not put in compilerOptions object.

Now allowSyntheticDefaultImports work fine with:

import Realm from "realm"

@CacaoRick Not sure how to interpret you. Does it work for you?

Yes allowSyntheticDefaultImports is work.
Thanks.

Was this page helpful?
0 / 5 - 0 ratings