-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Extended type parameters don't work as constraints with overloads #7378
Comments
sounds like a fair check to add. |
I think that this would be a good change, but it would make implementation/overload assignability diverge further from standard assignability rules on signatures. From 3.11.4 in the spec:
The important part is "are instantiated using type Any as the type argument for all type parameters". We look for the "erased" versions of each function. This is not to say we couldn't change the current behavior. I'd be curious to see how code is affected, but it would be a breaking change. |
similar case: interface I {
toString(): number;
}
function f(p: void) // erorr
function f(p: number)
function f(p: string) // erorr
function f(p: I) // erorr
function f(p: number) { } I expect a behavior that has consistency. function f<T extends number>(p: T) { }
f(''); // error interface I {
toString(): number;
}
function f<T extends number>(p: void)
function f<T extends number>(p: number)
function f<T extends number>(p: string)
function f<T extends number>(p: I)
function f<T extends number>(p: T) { }
f(''); // ok Overloads shouldn't widen a range of type parameters. Additionally, this behavior omits a feature of non-nullable types. |
This somewhere between too complex and "not actually wrong". For example: function f<T extends number>(p: void)
function f<T extends number>(p: number)
function f<T extends number>(p: string) // <--
function f<T extends number>(p: T) { } In the indicated line, The real purpose of the overload checking is to stop people from doing this (which they still manage to do with sad regularity): function fn(x: string): void;
function fn(x: number) { }
fn(42); // Not OK, wat?? Outside of detecting the "I thought I had two signatures" case, we generally consider you to be "on your own" in terms of writing an implementation signature that is plausibly correct. |
TypeScript Version:
master
Code
The text was updated successfully, but these errors were encountered: