-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
strictFunctionTypes has different behavior with parameter types and return types #18963
Comments
I'm not sure this difference is intended or not. |
I think you're right, the following should be an error with declare class C {
static a(f: (x: C) => C): void;
}
declare class D extends C {
private p: void;
static a(f: (x: D) => D): void;
} We currently don't report an error because of overlap in functionality between strict function type checking and covariant checking for callback parameters (#15104). This causes us to always check the return type of a callback parameter bivariantly when we should actually check it contravariantly in strict function types mode. Definitely a corner case, but one that we should get right. |
So, it turns out we can't tighten the rules for the original example. If we did we would break covariance for a number of core types, such as interface Array<T> {
// ...
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;
// ...
} If we were to strictly (i.e. contravariantly) check the return position of that callback in However... It is the case that we aren't correctly checking callback parameters of regular function types in strict function types mode. For example, we should error on both assignments below, but we only error on the second one: declare class C {
private c: void;
}
declare class D extends C {
private d: void;
}
declare let f1: (f: (x: C) => C) => void;
declare let f2: (f: (x: D) => D) => void;
f1 = f2; // Should be an error
f2 = f1; // Error |
cc @ahejlsberg
TypeScript Version: master
Code
Expected behavior:
no error or both
a
andb
make an error.Actual behavior:
The text was updated successfully, but these errors were encountered: