-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Regression: Covariance fails in latest TS release? #29590
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
Comments
It might work as intended, but I'm not sure why You can also look at typed-inject. I've added a test for covariance. Running it with ts 3.2.2 works fine, 3.2.4 (or 3.3.0-rc) gives the error. |
I think, but not positive, that type parameters are (supposed to be) completely invariant in TS. There is an open proposal to add co- and contravariance annotations a la C#, see #10717, but AFAICT nothing has been implemented yet. |
I don't think that's true. See this comment by @ahejlsberg #10717 (comment) |
Smaller repro? interface Injector<TContext> {
resolve: TContext[keyof TContext];
}
const foo: Injector<{ foo: string, bar: string }> = {} as any;
const bar: Injector<{ bar: string }> = foo;
console.log(bar); |
@jack-williams I don't think that's a reproduction of my issue. Your code example already gave a compile error in [email protected] (just tested it). However, it might indicate that it my example also should have been a compile error in [email protected]. |
Ooops, sorry --- got carried away deleting code! What about this? interface Injector<TContext> {
resolve: <T extends keyof TContext>() => T extends keyof TContext ? TContext[T] : never;
}
const foo: Injector<{ foo: string, bar: string }> = {} as any;
const bar: Injector<{ bar: string }> = foo;
console.log(bar); There have been a few recent changes to conditional types; it may the be the case that one of those have affected this. Seems to work for type aliases, but not interfaces: type Injector<TContext> = {
resolve: <T extends keyof TContext>() => T extends keyof TContext ? TContext[T] : never;
}
const foo: Injector<{ foo: string, bar: string }> = {} as any;
const bar: Injector<{ bar: string }> = foo; // no error
console.log(bar); |
@jack-williams
Do you think this was an unintended side effect? Or is there a reason for this change? |
This is basically a design limitation. In |
Ok. I'm assuming that the fact that it didn't report errors in 3.2.2 was a bug, and it now works as intended. Closing because it is a design limitation.
Got it, thanks for replying! |
TypeScript Version: 3.3.0-dev.20190125
Search Terms:
covariance
Code
Expected behavior:
No compiler error with
--strictFunctionTypes
. As was the case in TS 3.2.2Actual behavior:
Compiler error (since TS 3.2.4)
Playground Link:
https://www.typescriptlang.org/play/index.html#src=type%20Injector%3CTContext%3E%20%3D%20%7B%0D%0A%20%20resolve%3A%20%3CT%20extends%20keyof%20TContext%3E()%20%3D%3E%20T%20extends%20keyof%20TContext%20%3F%20TContext%5BT%5D%20%3A%20never%3B%0D%0A%7D%0D%0A%0D%0Aconst%20foo%3A%20Injector%3C%7B%20foo%3A%20string%2C%20bar%3A%20string%20%7D%3E%20%3D%20%7B%7D%20as%20any%3B%0D%0Aconst%20bar%3A%20Injector%3C%7B%20bar%3A%20string%20%7D%3E%20%3D%20foo%3B%20%2F%2F%20no%20error%0D%0Aconsole.log(bar)%3B
Related Issues:
(maybe): #28752
The text was updated successfully, but these errors were encountered: