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
Fall back on default type parameter when inference does not yield a more suitable type #16229
Comments
Ah this would be great if it got fixed! |
Any plans to fix this one soon? Cheers |
Hey, this issue opened 2 years ago 👎 . Nobody doesn't fix this( |
So 3 years and still nothing?! |
This is just not what type parameter defaults are for—they very intentionally play no part in inference. I argue that you want to know when inference “fails.” Let me modify the original example very slightly: interface Box<T> {
get: () => T;
set: (x: T) => void;
}
declare function doSomethingWithBox<T = any>(box: Box<T>): T;
declare let box: Box<number> | Box<string>;
doSomethingWithBox(box); The difference here is that |
Thank you for even asking that 😄 I was looking for fallbacking generics and your |
TypeScript Version: 2.3.2
Code
Actual behavior:
An inference error is produced on the
f(val)
invocation.Expected behavior:
TypeScript should be able to accept this program.
There are two possible paths by which this program could be accepted.
T = number | string
.In essence, the
f(val)
call requires TypeScript solveO<number> | O<string> = O<T>
. This is solvable if TypeScript can prove thatO<number> | O<string>
is a subtype ofO<number | string>
which depends on the variance ofO
.It's possible that TypeScript is already doing the right thing here and failing to infer a type for
T
given what it knows.T = any
when inference forT
fails.I believe this would be a valid improvement to the existing logic. Given the presence of the default type for
T
and the fact thatval
satisfies it, the given default ofany
should be used as inference has failed to identify a narrower type.It is unexpected that the compiler ignores the provided default for
T
which would allow the program to be accepted in favor of rejecting the program because inference is unable to solve forT
.Real world example:
Angular is attempting to add generics to our Forms API in a backwards-compatible manner, using
any
as a default generic type for the values of composed form structures.Here is the basic class structure:
We have been unable to deploy this solution as it breaks the following existing example:
Here, the array passed to the
FormArray
constructor has a union type (similar toval
in the theoretical example above), and inference forFormArray
's genericT
parameter fails. In this case, the default parameter type ofany
could be correctly applied and would ensure this code is still accepted (as it was before the addition of generics).Thanks to @rkirov for his insight and the simplified theoretical example, and @Toxicable for the initial report.
The text was updated successfully, but these errors were encountered: