-
Notifications
You must be signed in to change notification settings - Fork 99
[@types/ember bug] - ember types are broken in TypeScript 3.1 #246
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
[@types/ember bug] - ember types are broken in TypeScript 3.1 #246
Comments
Interestingly, TS |
I've also tripped on this: DefinitelyTyped/DefinitelyTyped#28114 Specifically: https://travis-ci.org/DefinitelyTyped/DefinitelyTyped/builds/415969505#L3391 |
This issue is blocking any new changes to My fix may involve walking back a significant amount of the existing computed property and |
I looked at fixing it on #27993 but it's way beyond my basic TS skills. |
@BryanCrotaz this is why I'm giving the original authors a chance to jump in and fix it before I try a more heavy-handed solution. There is some significant complexity around the current implementation of mixins/computed properties/ |
@mike-north one option to consider here, along with some of the other pieces we've discussed, is taking this as an opportunity to make a breaking change which jumps minimum supported TS version—in line with our official policy which is "(at least) latest two versions" we could bump to 2.8 and start leaning on conditional types. My and @dfreeman's initial experiments when 2.8 came out suggested we could pretty simplify things quite a bit around computed properties and getters using conditional types and |
My brief testing suggests that TS 2.8 |
Sounds like this is something that has already been brought to @Andy-MS's attention First, all recent versions of TS agree on this ([] as Person[]).firstObject // regarded as ComputedProperty<Person, Person> ✅ Where things get interesting is the type of the TypeScript 3.0.1Ember.Observable.get<{
length: number;
toString: any;
toLocaleString: any;
pop: any;
push: any;
concat: any;
join: any;
reverse: any;
shift: any;
slice: any;
sort: any;
splice: any;
unshift: any;
indexOf: any;
lastIndexOf: any;
every: any;
... 74 more ...;
frozenCopy: any;
}, "firstObject">(this: ComputedPropertyGetters<...>, key: "firstObject"): Person | undefined TypeScript 3.1.0-dev.20180821Ember.Observable.get<Person[], "firstObject">(this: (Person | Ember.ComputedProperty<Person, any> | ComputedProperty<Person, any>)[], key: "firstObject"): Ember.ComputedProperty<Person | undefined, Person | undefined> If you look at the // ComputedPropertyGetters<Person[]> ⤵
Ember.ComputedProperty<Person[], any> | ModuleComputed<Person[], any> | Person[] and we now get // ComputedPropertyGetters<Person[]> ⤵
(Ember.ComputedProperty<Person, any> | ModuleComputed<Person, any> | Person)[] @dwickern I don't see how |
Before TS 2.8 this was the only way to pull the underlying type ComputedPropertyGetters<T> = { [K in keyof T]: ComputedProperty<T[K]> | T[K] };
function get<T, K extends keyof T>(obj: ComputedPropertyGetters<T>, key: K): T[K]; It was not very reliable before (microsoft/TypeScript#20280) and now there is this new interaction with arrays and unions (microsoft/TypeScript#26120). The equivalent implementation using type UnwrapComputedPropertyGetter<T> = T extends ComputedProperty<infer U> ? U : T;
function get<T, K extends keyof T>(obj: T, key: K): UnwrapComputedPropertyGetter<T[K]>; I pushed my WIP to a branch here: |
@dwickern This context from your original intent is super helpful. I believe that I have a working fix built on top of your branch. I just need to propagate everything through and add some more test cases. |
I ran into an interesting situation where it looks like TypeScript may be discarding some information. Currently our If we simplify this to the typical // kind of like our Ember.ComputedProperty
type Box<T, U = T> = { value: any };
// a ComputedProperty<string> that's clobbered with a string via .extend() or .create()
type MaybeBoxed<T> = Box<T> & T;
// kind of like @dwickern's approach to getting the value type from
// something that **might** be a CP (or might not)
type UnBox<S> = S extends Box<infer T> ? T : S;
// a concrete example for us to think about
type MaybeBoxedString = Box<string> & string;
type Unboxed = UnBox<MaybeBoxedString>; // {} 🚨 -- would be nice if it were a string! This problem seems to be solved by using any generic anywhere type Box<T, U = T> = { value: any, __do_not_touch_this: T };
type MaybeBoxed<T> = Box<T> & T;
type UnBox<S> = S extends Box<infer T> ? T : S;
type MaybeBoxedString = Box<string> & string;
type Unboxed = UnBox<MaybeBoxedString>; // string ✅ Thankfully the type in question is a class, so we can use a private field for this and no harm is done |
Ah, yes, the generic types need to be used somewhere. Otherwise a |
Ahhhh, that's why that was falling over that way. I had never gotten far enough down the rabbit hole to figure it out, but it drove nuts when I first spiked this with |
PR opened |
Fix for a variety of ember types has been merged |
Which package(s) does this problem pertain to?
What are instructions we can follow to reproduce the issue?
Now about that bug. What did you expect to see?
I expected
tsc
to compile the ambient types and test files successfullyWhat happened instead?
NOTE
Switching to a TypeScript 2.x
will compile the tests successfully and without error.
The text was updated successfully, but these errors were encountered: