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

linkingObjects() TS type inference + class-based models #5326

Closed
nlarew opened this issue Jan 25, 2023 · 2 comments
Closed

linkingObjects() TS type inference + class-based models #5326

nlarew opened this issue Jan 25, 2023 · 2 comments

Comments

@nlarew
Copy link
Contributor

nlarew commented Jan 25, 2023

How frequently does the bug occur?

Always

Description

The Realm.Object.linkingObjects() method, when given a class-based model as a type argument, does not infer the correct TypeScript type for the collection that it returns.

The method does not allow you to specify the class-based model, though this would be a welcome improvement and in line with the API for Realm.objects, useObject, etc.

// ✅ The useObject hook correctly infers the `Post` type based on the class I pass in
const post = useObject(Post, _id)!; // Post & Realm.Object<Post, never>

// ⛔️ This doesn't work - the first arg must be a string - but it would be ideal
const user = post.linkingObjects(User, 'posts')[0]!; // User & Realm.Object<User, never>

// ‍ This runs but it uses the class _name_ as a string and doesn't infer the correct return type
const user = post.linkingObjects('User', 'posts')[0]!; // Realm.Object<unknown, never>

// ‍ This runs and assigns a half correct type but still uses class name + a type cast
const user = post.linkingObjects<User>('User', 'posts')[0]!; // User & Realm.Object<unknown, never>

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

  1. Create a Realm.Object class with a relationship to another class.
 class Foo extends Realm.Object {
   _id: Realm.BSON.ObjectId,
   name: string;
   bars: Realm.List<Bar>
   
   static schema = {
     name: 'Foo',
     properties: {
       _id: 'objectId',
       name: 'string',
       bars: 'Bar[]',
     },
     primaryKey: '_id',
   };
 }
 
 class Bar extends Realm.Object {
   _id: Realm.BSON.ObjectId,
   name: string;
   
   static schema = {
     name: 'Bar',
     properties: {
       _id: 'objectId',
       name: 'string',
     },
     primaryKey: '_id',
   };
 }

await Realm.open({ schema: [Foo.schema, Bar.schema] })
  1. Add objects that instantiate the relationship
realm.write(() => {
  const foo = realm.create(Foo, { _id: new Realm.BSON.ObjectId(), name: "Winnie the Foo Bear" })
  const bar1 = realm.create(Bar, { _id: new Realm.BSON.ObjectId(), name: "Honey" })
  const bar2 = realm.create(Bar, { _id: new Realm.BSON.ObjectId(), name: "Smile" })
  foo.bars.push(bar1)
  foo.bars.push(bar2)
})
  1. Get a collection of backlinks for the relationship using linkingObjects()
const bars = realm.objects(Bar);
const bar0 = bars[0]!
// ERROR: Argument of type 'typeof User' is not assignable to parameter of type 'string'.
const linkingFoos = bar0.linkingObjects(Foo, "bars")!;

// BUG: TS Type is not correct
const linkingFoos: Realm.Results<Realm.Object<unknown, never>> = bar0.linkingObjects('Foo', "bars")!;

Version

11.4.0

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

macOS Monterey (12.6.2)

Build environment

No response

Cocoapods version

No response

@kneth
Copy link
Contributor

kneth commented Jan 26, 2023

@nlarew Thank for reporting. I agree, that the TS types for linkingObjects() need an update (by looking at https://github.com/realm/realm-js/blob/master/types/index.d.ts#L363

@papafe
Copy link
Contributor

papafe commented Mar 17, 2023

This has been fixed with #5487

@papafe papafe closed this as completed Mar 17, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 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

3 participants