ember-cli-typescript
ember-cli-typescript copied to clipboard
`PromiseObject<T>` should extend `PromiseProxyMixin<T | null>`, not `PromiseProxyMixin<T & ObjectProxy>`
Based on a Discord conversation with @chriskrycho on 2020.02.17.
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/d129b9b777901659b1559f9355f9548496e72a2b/types/ember-data/index.d.ts#L919:L921
Which package(s) does this problem pertain to?
- [x] @types/ember-data
What are instructions we can follow to reproduce the issue?
given:
class Comment extends Model {
@belongsTo('post')
post!; DS.PromiseObject<Post>;
}
Now about that bug. What did you expect to see?
Resolving that promise should result in an object of type <Post | null>.
What happened instead?
The type hints in vscode indicate instead that it would Post & ObjectProxy<object>.
It would be nice if the expected nullability (from the API) could be specified, so that the returned value was either Post or Post | null; but in either case the ObjectProxy<object> should not be part of the returned types.
A couple observations here—
-
I don’t think the resolved type should be
T & ObjectProxy<object>—at a minimum it probably needs to beT & ObjectProxy<T>, but I think it shouldn't beObjectProxyat all if it has beenawait-ed or within athencallback. -
We need to support opting out of a
null(orundefined?) resolved type, while letting that be in the default for it. My thought process here is: we want to capture the real default behavior for Ember Data, but we also want to let users say “No, my API will never benullin this response.”
A related but important question, as I haven’t poked at this particular behavior of Ember Data in a while: if you await someModel.someAysncRelationship and the lookup fails—e.g. there’s a network timeout—what actually happens? Does it throw an exception, or does it resolve as undefined or null?
If an await call to the API fails, it throws an exception into the catch block. (Or if resolved with then, into the catch handler.)
Thanks. That matches what I expected!
I think this got resolved in Ember Data’s types on DT? Feel free to reopen if not/still relevant.