question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What type does Realm Objects have in TypeScript compiling Node

See original GitHub issue

Goals

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:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
knethcommented, May 16, 2018

What is your TSC options? I can transpile and run the following code:

import * as Realm from "realm";

class Video {
    public static schema: Realm.ObjectSchema = {
        name: 'Video',
        primaryKey: 'youtubeID',
        properties: {
            youtubeID:     'string',
            title:         'string',
            filePath:      'string?',
            thumbnailPath: 'string?',
            uploadDate:    'date'
        }
    }

    public youtubeID:      string;
    public title:          string;
    public filePath?:      string | null;
    public thumbnailPath?: string | null;
    public uploadDate:     Date;

    constructor(youtubeID: string, title: string, uploadDate: Date) {
        this.youtubeID = youtubeID;
        this.title = title;
        this.uploadDate = uploadDate;
    }
};

let realm = new Realm({schema: [Video.schema]});
realm.write(() => {
    let video = new Video('foo', 'bar', new Date());
    realm.create(Video.schema.name, video);
});

console.log('Done', realm.objects(Video.schema.name));
realm.close();
10reactions
astigsencommented, Aug 24, 2018

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:

class Video {
    public static schema: Realm.ObjectSchema = {
        name: 'Video',
        primaryKey: 'youtubeID',
        properties: {
            youtubeID:     'string',
            title:         'string',
            filePath:      'string?',
            thumbnailPath: 'string?',
            uploadDate:    'date'
        }
    }

    public youtubeID:      string;
    public title:          string;
    public filePath?:      string | null;
    public thumbnailPath?: string | null;
    public uploadDate:     Date;
}

const videos = realm.objects<Video>(Video.schema.name);
for (let i=0;i<videos.length;i++){
    videos[i].title = "a";
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Define a Realm Object Schema - Node.js SDK - MongoDB
Every property in a Realm object has a strongly defined data type. A property's type can be a primitive data type or an...
Read more >
realm - npm
Realm is a mobile database that runs directly inside phones, tablets or wearables. This project hosts the JavaScript versions of Realm.
Read more >
Node.js v19.3.0 Documentation
What does it mean to "contextify" an object? Timeout interactions with asynchronous tasks and Promises. WebAssembly System Interface (WASI). Class: WASI. new ...
Read more >
Getting started with Realm for React Native | by mbvissers.eth
Realm works with Schemas. For simplicity we will make one Database.js file with all our Realm code from which we can export functions...
Read more >
How to Setup a TypeScript + Node.js Project | Khalil Stemmler
Since we're using Node, it would be good if we could get type ... The tsconfig.json is where we define the TypeScript compiler...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found