ember-cli-typescript icon indicating copy to clipboard operation
ember-cli-typescript copied to clipboard

`PromiseObject<T>` should extend `PromiseProxyMixin<T | null>`, not `PromiseProxyMixin<T & ObjectProxy>`

Open oliverlangan opened this issue 5 years ago • 3 comments

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.

oliverlangan avatar Feb 18 '20 05:02 oliverlangan

A couple observations here—

  1. I don’t think the resolved type should be T & ObjectProxy<object>—at a minimum it probably needs to be T & ObjectProxy<T>, but I think it shouldn't be ObjectProxy at all if it has been await-ed or within a then callback.

  2. We need to support opting out of a null (or undefined?) 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 be null in 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?

chriskrycho avatar Feb 18 '20 15:02 chriskrycho

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.)

oliverlangan avatar Feb 18 '20 17:02 oliverlangan

Thanks. That matches what I expected!

chriskrycho avatar Feb 19 '20 03:02 chriskrycho

I think this got resolved in Ember Data’s types on DT? Feel free to reopen if not/still relevant.

chriskrycho avatar Sep 28 '23 23:09 chriskrycho