-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Covariant checking for callback parameters #15104
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
@mhegazy Want to take a look? |
@ShawnTalbert If not clear. This PR does not introduce any new syntax. Instead changes type assignability of generics where the type is only user in a function parameter position e.g Promise / Observable interface Box<T> {
foo(cb: (t: T => void)): void;
} Before: declare const bA: Box<Animal>;
const bC: Box<Cat> = bA; // no error 😿 After: declare const bA: Box<Animal>;
const bC: Box<Cat> = bA; // error 😺 |
Yes, wrong issue, sorry |
It appears that the solution was amended to ignore union with null/undefined. Should that be dependent on --strictNullChecks? At the moment, this does not seem to address #13513. |
@bmcbarron #13513 is fixed by this PR in |
never
instead of void
for Observable type arguments
DefinitelyTyped/DefinitelyTyped#17248
This doesn't fix this problem unfortunately and this is usually the case if the callbacks are stored in different files and imported. class Animal {}
class Cat extends Animal {
public meow() {}
}
let promise: Promise<Animal> = null;
promise.then((cat: Cat) => { //Should error here but does not
cat.meow();
}); |
In order to ensure that any generic type
Foo<T>
is at least co-variant with respect toT
no matter howFoo
usesT
, TypeScript relates parameters bi-variantly (given that parameters are input positions, they naturally relate only contra-variantly). However, when source and target parameters both have function types with a single call signature, we know we are relating two callback parameters. In that case it is sufficient to only relate the parameters of the signatures co-variantly because, similar to return values, callback parameters are output positions. With this PR we introduce that change. This means that aPromise<T>
orObservable<T>
, whereT
is used only in callback parameter positions, will be co-variant (as opposed to bi-variant) with respect toT
, which solves a commonly reported issue.This change finds several issues in our real world code suites that all appear to be legitimate inconsistencies.
Fixes #11022.
Fixes #14770.