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

TypeScript support for "instanceof {List, Result, Collection}"

      Problem

      In its current state, it's not possible to use the instanceof operator to check if the value of a property is a Realm.List, Realm.Result or Realm.Collection:

      This simple playground implements the current types and show the issue:

      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
      // API
      declare namespace Realm {
          interface Collection {}
          const Collection: {};
      }
      declare class Realm {}
      
      // Usage
      const collection = Realm.Collection;
      if (collection instanceof Realm.Collection) { //   will produce a TS error
      
      }
      

      Solution

      One alternative is to declare classes on the Realm namespace:

      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
      // API
      declare namespace Realm {
          interface Collection {}
          class Collection implements Collection {}
      }
      declare class Realm {}
      
      // Usage
      const collection = Realm.Collection;
      if (collection instanceof Realm.Collection) {//   works
      
      }
      

      Another alternative is to simplify our type by completely abandoning the use of the TypeScript namespace and expose these classes as static properties on the Realm class:

      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
      // API
      declare class Collection {}
      declare class Realm {
          static Collection: typeof Collection;
      }
      
      // Usage
      const collection = Realm.Collection;
      if (collection instanceof Realm.Collection) {//   works
      
      }
      

      This would be a breaking change, since developers relying on the global "Realm" type (declared by the namespace) would have to start explicitly importing types from "realm".

      How important is this improvement for you?

      I'd like to see it, but have a workaround

            Assignee:
            Unassigned Unassigned
            Reporter:
            kraen.hansen@mongodb.com Kræn Hansen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: