Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-310

Typescript Initialisation of List properties

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None

      Goals

      Say I've got a schema like this (in Typescript):

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      export class Stuff {
      	static schema: ObjectSchema = {
      		name: 'Stuff',
      		primaryKey: 'id',
      		properties: {
      			id: 'string',
      			text: 'string[]',
      		},
      	};
      
      	id: string;
      	text: List<string>;
      }
      

      This is a compiler error because id and text aren't optional and aren't instantiated in the constructor.

      But in this case, what would the constructor actually look like? This is the best I can guess:

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      constructor() {
      	this.id = 'item-1';
      	this.text = new List<string>();
      }
      

      but

      Cannot use 'new' with an expression whose type lacks a call or construct signature.ts(2351)
      

      Expected Results

      How do we correctly initialise objects with required lists so the Typescript types match up to the actual object? The goal is to be able to do this:

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      const stuff = new Stuff();
      stuff.text.push('text here');
      

      so if we don't initialise text with anything then it won't be there and we'll be calling push on undefined.

      What should the constructor for this model look like?

      Actual Results

      Can't figure out how to correctly construct an object with Linking Objects or List property.

      Related Stack Overflow

      I didn't ask this question on stack overflow because this question is already there and hasn't been answered for a month that didn't get an answer.

      Version of Realm and Tooling

      • Realm JS SDK Version: 2.29.1
      • Typescript Version: 3.5.3
      • Node or React Native: Doesn't matter
      • Client OS & Version: Doesn't matter
      • Which debugger for React Native: Doesn't matter

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: