Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type 'ListType<string> | List<string[]>' is not assignable to type '(string | Unmanaged<string, never>)[] | undefined'. #6292

Closed
szkieb opened this issue Dec 1, 2023 · 6 comments

Comments

@szkieb
Copy link

szkieb commented Dec 1, 2023

How frequently does the bug occur?

Always

Description

Creating a class with one property being an array of strings for which I used the RealmList type. I expect the interface, schema and constructor to work together and be compatible but the types don't match up.

Stacktrace & log output

Type 'ListType<string> | List<string[]>' is not assignable to type '(string | Unmanaged<string, never>)[] | undefined'.
  Type 'List<string>' is missing the following properties from type '(string | Unmanaged<string, never>)[]': reverse, sort, fill, copyWithin, and 6 more.ts(2322)

Can you reproduce the bug?

Always

Reproduction Steps

I write a class like in the example below.

export class MyClass extends Realm.Object<MyClass> {
   myClassId: string;
   ...
  images!: Realm.List<string>;
   ...

public static schema: Realm.ObjectSchema = {
name: 'MyClass',
    primaryKey: 'myClassId',
    properties: {
      myClassId: string,
   ...
      images: { type: 'list', objectType: 'string', optional: false },
   ...
    }
  }
 constructor(realm: Realm, properties: Partial<Report>) {
    super(realm, {
       myClassId: properties.myClassId || new BSON.ObjectID().toString(),
   ...
       images: properties.images || new Realm.List<string[]>(),
   ...

I set a default value in the constructor resulting in this error.

Type 'ListType<string> | List<string[]>' is not assignable to type '(string | Unmanaged<string, never>)[] | undefined'.
  Type 'List<string>' is missing the following properties from type '(string | Unmanaged<string, never>)[]': reverse, sort, fill, copyWithin, and 6 more.ts(2322)

What is funny is even though I define the property as realm list and I construct it as such it somehow complains that it ought be 'unmanaged' and unmanaged is not well documented as far as I can tell. If I am doing something wrong I would at like the documentation to be clearer on (1) best practices or (im)possibilities of creating string arrays as properties and (2) creating default values in typescript esp. complex types such as realm lists and embedded objects.

Version

"realm": "^12.3.1",

What services are you using?

Local Database only

Are you using encryption?

Yes

Platform OS and version(s)

iOS 17, expo 48

Build environment

No response

Cocoapods version

No response

edited example code

@kneth
Copy link
Contributor

kneth commented Dec 1, 2023

Should class Report extends Realm.Object<MyClass> be class MyClass extends Realm.Object?

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Dec 1, 2023
@szkieb
Copy link
Author

szkieb commented Dec 1, 2023

Should class Report extends Realm.Object<MyClass> be class MyClass extends Realm.Object?

Yes, it should. I tried to make my example generic and forgot to edit that part.

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Dec 1, 2023
@kneth kneth closed this as completed Dec 4, 2023
@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Dec 4, 2023
@szkieb
Copy link
Author

szkieb commented Dec 5, 2023

Hi @kneth I am sorry if my response was misleading. The issue persists. I still cannot assign a realm list to a property of type realm list. I assume it is somewhat related to #5157 and how realm types and realm class-based models interact with typescript.

@sync-by-unito sync-by-unito bot reopened this Dec 5, 2023
@kneth
Copy link
Contributor

kneth commented Dec 5, 2023

I have reopened the issue

@elle-j
Copy link
Contributor

elle-j commented Feb 14, 2024

Hi @szkieb, the Realm.List constructor is not meant to be used by the user when constructing a list (it's only used internally). Since you want to use an empty list as a default value you can simply use the JavaScript Array literal [] (this is the "unmanaged" list). Once you query the realm and access the list, it will be a Realm.List (the managed list).

Since you want to use default values, an alternative to what you have is to define the defaults directly in the schema (also note that you don't need to explicitly set optional: false to the images since that is the default behavior):

class MyClass extends Realm.Object<MyClass> {
  // Don't forget the `!`.
  myClassId!: string;
  images!: Realm.List<string>;

  static schema: Realm.ObjectSchema = {
    name: "MyClass",
    primaryKey: "myClassId",
    properties: {
      myClassId: { type: "string", default: () => new BSON.ObjectId().toString() },
      images: { type: "list", objectType: "string", default: [] },
    },
  };

  constructor(realm: Realm, properties: Partial<Report>) {
    super(realm, properties);
  }
}

// After having opened the realm...

realm.write(() => {
  new MyClass(this.realm, {
    // Any required properties..
  });
});

It's also common to skip the constructor in the class and pass in object literals when adding it to the realm:

class MyClass extends Realm.Object<MyClass> {
  myClassId!: string;
  images!: Realm.List<string>;

  static schema: Realm.ObjectSchema = {
    name: "MyClass",
    primaryKey: "myClassId",
    properties: {
      myClassId: { type: "string", default: () => new BSON.ObjectId().toString() },
      images: { type: "list", objectType: "string", default: [] },
    },
  };
}

// After having opened the realm...

realm.write(() => {
  realm.create(MyClass, {
    // Any required properties..
  });
});

Let us know if this helps you.

@nirinchev
Copy link
Member

Closing due to inactivity.

@nirinchev nirinchev closed this as not planned Won't fix, can't repro, duplicate, stale Mar 20, 2024
@sync-by-unito sync-by-unito bot closed this as completed Mar 20, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants