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

TypeScript: `Type 'unknown' is not assignable to type {classname}`, possibly related to `Realm.Object` definition

      How frequently does the bug occur?

      Always

      Description

      I was trying to use types for a collection change listener function, and I am getting a waterfall of errors, with the title being the last line, from the addListener.

      I see the Realm Object has the following type definition, but I am failing the reason for defaulting the type to "unknown" (I am not a beginner, but not a pro yet).

      https://github.com/realm/realm-js/blob/aefbe59a5ab9642bc89b157e0491a7339f0cba8a/types/index.d.ts#L329

      Removing it from the Realm Object description removes the error, but I do not know if it serves to prevent any disaster. Please, advise why it happens and/or remove this defaulting part. (or maybe open a new issue to TS repo if it is on their side)

      Stacktrace & log output

      Unable to find source-code formatter for language: shell. 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
      unknown.ts:39:25 - error TS2345: Argument of type 'CollectionChangeCallback<Object<Task, never>>' is not assignable to parameter of type 'CollectionChangeCallback<Object<unknown, never>>'.
        Type 'Object<Task, never>' is not assignable to type 'Object<unknown, never>'.
          Types of property 'addListener' are incompatible.
            Type '(callback: ObjectChangeCallback<Task>) => void' is not assignable to type '(callback: ObjectChangeCallback<unknown>) => void'.
              Types of parameters 'callback' and 'callback' are incompatible.
                Types of parameters 'changes' and 'changes' are incompatible.
                  Type 'ObjectChangeSet<Task>' is not assignable to type 'ObjectChangeSet<unknown>'.
                    Type 'unknown' is not assignable to type 'Task'.
      
      39     watcher.addListener(listener);
                                 ~~~~~~~~
      

      Can you reproduce the bug?

      Always

      Reproduction Steps

      Reproducing:
      1- create a new folder, and open a terminal into it
      2- npm install typescript realm @types/node (no need to init)
      3- copy the next code to a file, ie unknown.ts
      4- to get the error, run npx tsc unknown.ts --target es6 --module es6 --moduleResolution node

      Unable to find source-code formatter for language: ts. 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
      import * as Realm from "realm";
      
      const TaskSchema: Realm.ObjectSchema = {
          name: "Task",
          properties: {
              id: { type: "int" },
              name: "string",
          },
      }
      class Task extends Realm.Object<Task> {
          public static schema: Realm.ObjectSchema = TaskSchema
          constructor(realm: Realm, public id: Realm.Types.Int, public name: Realm.Types.String) {
              super(realm, { id, name })
          }
      }
      
      const realm_config: Realm.Configuration = {
          path: "realm-files/myrealm",
          schema: [TaskSchema],
      }
      async function openRealm() {
          const realm = await Realm.open(realm_config);
          const watcher = realm.objects(Task.schema.name)
          const listener: Realm.CollectionChangeCallback<Realm.Object<Task>> = function (collection: Realm.Collection<Realm.Object<Task, never>>, changes: Realm.CollectionChangeSet): void {
              changes.deletions.forEach((index) => {
                  console.log(`A task was deleted at the ${index} index`);
              });
              changes.insertions.forEach((index) => {
                  let insertedTasks = collection[index];
                  console.log(
                      `insertedTasks: ${JSON.stringify(insertedTasks, null, 2)}`
                  );
              });
              changes.newModifications.forEach((index) => {
                  let modifiedTask = collection[index];
                  console.log(`modifiedTask: ${JSON.stringify(modifiedTask, null, 2)}`);
              });
          }
          watcher.addListener(listener);
          return realm
      }
      const realm = openRealm()
      

      Version

      "realm": "^11.5.0",

      What services are you using?

      Local Database only

      Are you using encryption?

      No

      Platform OS and version(s)

      Windows 10 21H2, NodeJS v18.9.0, TypeScript 4.9.5

      Build environment

      No response

      Cocoapods version

      No response

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

              Created:
              Updated:
              Resolved: