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

[ember-data] references API is not compatible with [email protected] and higher #1416

Open
1 of 21 tasks
ro0gr opened this issue Mar 17, 2021 · 5 comments
Open
1 of 21 tasks
Labels
types:core:data Something is wrong with the Ember Data type definitions types:core Something is wrong with the Ember type definitions

Comments

@ro0gr
Copy link
Contributor

ro0gr commented Mar 17, 2021

The problem appears when you try to use this.belongsTo( or this.hasMany( in scope of a model method or accesor. In this case this is broken, and it complains about the invalid invocation, until this type is explicitly specified in the method signature.

Unfortunately, an explicit this workaround doesn't work for accesors in [email protected] and higher, since they forbidden to specify this on getters/setters, see microsoft/TypeScript#39254 (comment).

Which package(s) does this problem pertain to?

  • @types/ember
  • @types/ember__string
  • @types/ember__polyfills
  • @types/ember__object
  • @types/ember__utils
  • @types/ember__array
  • @types/ember__engine
  • @types/ember__debug
  • @types/ember__runloop
  • @types/ember__error
  • @types/ember__controller
  • @types/ember__component
  • @types/ember__routing
  • @types/ember__application
  • @types/ember__test
  • @types/ember__test-helpers
  • @types/ember__service
  • @types/ember-data
  • @types/rsvp
  • Other
  • I don't know

What are instructions we can follow to reproduce the issue?

export default class User extends Model {
  @belongsTo("user")
  friend!: AsyncBelongsTo<User>;

  // `this: User` leads to the following compilation error:
  // > get' and 'set' accessors cannot declare 'this' parameters.t
  get friendId(this: User) {
    // if don't specify `this`, it'd fail on the next line
    return this.belongsTo("friend").value()?.id;
  }
}
Reproduction Case

Link: https://codesandbox.io/s/my-app-forked-zqghf?file=/app/models/user.ts

Now about that bug. What did you expect to see?

I expect:

export default class User extends Model {
  @belongsTo("user")
  friend!: AsyncBelongsTo<User>;

  get friendId() {
    return this.belongsTo("friend").value()?.id;
  }
}

to work w/o a need for explicit this and compilation errors.

What happened instead?

If remove an explicit this from the signature,

  return this.belongsTo("friend").value()?.id;

would lead to a compilation error:

Argument of type 'string' is not assignable to parameter of type 'Exclude<keyof this, "isEmpty" | "isLoading" | "isLoaded" | "hasDirtyAttributes" | "isSaving" | "isDeleted" | "isNew" | "isValid" | "dirtyType" | "isError" | "isReloading" | "id" | ... 41 more ... | "toString">'.ts(2345)
Compilation error:

@ro0gr
Copy link
Contributor Author

ro0gr commented Mar 17, 2021

A while ago, I did a PR(DefinitelyTyped/DefinitelyTyped#42839), which fixed the belongsTo(, hasMany( return types. And as a side effect, it did also fix this corrupted this issue.

Unfortunately it has never been merged. Happy to reopen it.

@chriskrycho chriskrycho added the types:core Something is wrong with the Ember type definitions label Oct 26, 2021
@chriskrycho
Copy link
Member

@ro0gr thanks for writing this up and sorry that PR ended up languishing. If you reopen it, I’ll help you get it landed!

@ro0gr
Copy link
Contributor Author

ro0gr commented Oct 26, 2021

@chriskrycho thanks for the heads up! Glad to hear that.

Ok, I'll give it another try this weekend.

@ewal
Copy link

ewal commented Apr 5, 2022

A workaround is to use call and pass the reference as an argument, like such

return this.belongsTo.call(this, 'friend').value().id
// Or 
return this.belongsTo.call(this, 'friend').id()

@JarrettSpiker
Copy link

My workaround ended up being

  get friendId() {
    let inst = this as User;
    return this.belongsTo("friend").value()?.id;
  }

though I suspect I may be encountering a slightly separate issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types:core:data Something is wrong with the Ember Data type definitions types:core Something is wrong with the Ember type definitions
Projects
None yet
Development

No branches or pull requests

4 participants