You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Extended type parameters don't work as constraints with overloads #7378
interfaceI{toString(): number;}functionf<Textendsnumber>(p: void)// should be an errorfunctionf<Textendsnumber>(p: number)functionf<Textendsnumber>(p: string)// should be an errorfunctionf<Textendsnumber>(p: I)// should be an errorfunctionf<Textendsnumber>(p: T){}
I think that this would be a good change, but it would make implementation/overload assignability diverge further from standard assignability rules on signatures.
[A type] S is _assignable to_ a type T, and T is _assignable from_ S, if S has no excess properties with respect to T (3.11.5) and one of the following is true:
S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, one of the following is true:
M is a non-specialized call or construct signature and S has an apparent call or construct signature N where, when M and Nare instantiated using type Any as the type argument for all type parameters declared by M and N (if any),
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.
In the indicated line, T might be the type string & number, which actually satisfies the constraint. There's also no relation between all the Ts here so it's not clear what the signature is trying to say -- when T doesn't appear in the parameter list or the return type, it's totally meaningless.
The real purpose of the overload checking is to stop people from doing this (which they still manage to do with sad regularity):
functionfn(x: string): void;functionfn(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.
Activity
mhegazy commentedon Mar 3, 2016
sounds like a fair check to add.
DanielRosenwasser commentedon Mar 3, 2016
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.
falsandtru commentedon Mar 3, 2016
similar case:
I expect a behavior that has consistency.
Overloads shouldn't widen a range of type parameters.
Additionally, this behavior omits a feature of non-nullable types.
RyanCavanaugh commentedon Jun 9, 2016
This somewhere between too complex and "not actually wrong". For example:
In the indicated line,
Tmight be the typestring & number, which actually satisfies the constraint. There's also no relation between all theTs here so it's not clear what the signature is trying to say -- whenTdoesn't appear in the parameter list or the return type, it's totally meaningless.The real purpose of the overload checking is to stop people from doing this (which they still manage to do with sad regularity):
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.