Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object is not a constructor on Typescript import * from "realm" #1226

Closed
CacaoRick opened this issue Aug 18, 2017 · 12 comments · Fixed by #3102
Closed

Object is not a constructor on Typescript import * from "realm" #1226

CacaoRick opened this issue Aug 18, 2017 · 12 comments · Fixed by #3102

Comments

@CacaoRick
Copy link

CacaoRick commented Aug 18, 2017

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

@kneth
Copy link
Contributor

kneth commented Aug 18, 2017

@CacaoRick
Copy link
Author

CacaoRick commented Aug 18, 2017

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.


@kneth
Copy link
Contributor

kneth commented Aug 21, 2017

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

@kneth
Copy link
Contributor

kneth commented Aug 22, 2017

@CacaoRick Is your issue related to #884?

@CacaoRick
Copy link
Author

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)

@BANG88
Copy link

BANG88 commented Sep 4, 2017

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

"allowSyntheticDefaultImports": true

@CacaoRick
Copy link
Author

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.

@BANG88
Copy link

BANG88 commented Sep 11, 2017

What version of your Realm types ?

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

Works fine for me.

@CacaoRick
Copy link
Author

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

@CacaoRick CacaoRick reopened this Sep 11, 2017
@CacaoRick
Copy link
Author

CacaoRick commented Sep 11, 2017

Sorry my allowSyntheticDefaultImports not put in compilerOptions object.

Now allowSyntheticDefaultImports work fine with:

import Realm from "realm"

@kneth
Copy link
Contributor

kneth commented Sep 12, 2017

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

@CacaoRick
Copy link
Author

Yes allowSyntheticDefaultImports is work.
Thanks.

@kneth kneth closed this as completed Sep 13, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants