question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ember-data] references API is not compatible with typescript@3.9 and higher

See original GitHub issue

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 typescript@3.9 and higher, since they forbidden to specify this on getters/setters, see https://github.com/microsoft/TypeScript/issues/39254#issuecomment-649831793.

Which package(s) does this problem pertain to?

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:

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JarrettSpikercommented, Jul 25, 2022

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

1reaction
ro0grcommented, Mar 17, 2021

A while ago, I did a PR(https://github.com/DefinitelyTyped/DefinitelyTyped/pull/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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ember v4 TypeScript Support Update
The minimum supported TypeScript version is 4.4. This aligns with our upcoming proposal for Ember's own TypeScript support policy, where major ...
Read more >
Semantic Versioning for TypeScript Types - Ember RFCs
TypeScript does not adhere to Semantic Versioning, but since it participates in the npm ecosystem, it uses the same format for its version ......
Read more >
Announcing the Official TypeScript Types Public Preview
As of ember-source@4.8.0-beta.2 , Ember is shipping a public preview of our official TypeScript support for the framework itself.
Read more >
Ember 3.8 Released
Today the Ember project is releasing version 3.8 of Ember.js, Ember Data, and Ember CLI. This release kicks off the 3.9 beta cycle...
Read more >
Introduction - Models - Ember Guides
js app with a server that does not have an adapter available (for example, you hand-rolled an API server that does not adhere...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found