What type does Realm Objects have in TypeScript compiling Node
See original GitHub issueGoals
I would like to be able to refer to Realm objects defined through an ObjectSchema with a concrete type rather than any
in TypeScript.
Expected Results
Be able to use explicit types for queries and Realm objects instead of having to use any
.
Actual Results
At the moment if I make a query, TypeScript will infer the type to be Results<{}>
and hence I cannot access the properties of the actual ObjectSchema
.
Steps to Reproduce
Create a model using an ObjectSchema
and query objects of that type.
Code Sample
This is one of my ObjectSchema
definitions:
const VideoSchema: Realm.ObjectSchema = {
name: 'Video',
primaryKey: 'youtubeID',
properties: {
youtubeID: 'string',
title: 'string',
filePath: 'string?',
thumbnailPath: 'string?',
uploadDate: 'date'
}
};
This is how I query the objects from Realm and try to access their properties:
const videos = realm.objects(VideoSchema);
for (let i=0;i<videos.length;i++){
videos[i].title = "a"; //Error 2339: Property 'title' does not exist on type '{}'
}
The error:
Error 2339: Property ‘title’ does not exist on type ‘{}’
The results are the same if I change my query to const videos=realm.objects('Video');
.
The only possible solution I have found so far was to explicitly cast the Results
instance to any
by const videos:any = realm.objects(VideoSchema)
.
It seems to me that the use of classes would solve my issue, however, according to the docs, defining models via classes is not available in Node yet.
I have just started using TypeScript, so I might be missing something, but I’d be glad if someone could shed some light on this issue for me.
Version of Realm and Tooling
- Realm JS SDK Version: 2.5.0
- Node or React Native: Node v8.9.4
- Client OS & Version: macOS 10.13.4 and Ubuntu 16.04.4
- Which debugger for React Native: N/A
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
What is your TSC options? I can transpile and run the following code:
You will need to define actual types for the objects in your schema so that Typescript know which types to return and expect.
So to update your code sample, you could do something like this: