-
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
--strictFunctionTypes claims incompatible types with generics #20702
Comments
Note that function f<T extends "a" | "b">(s: T): T {
switch (s) {
case "a": return "a";
case "b": return "b";
default: throw new Error();
}
} This is probably not an easy problem to solve... |
@andy-ms yeah, const still works as noted. About the parameter Your last example is much more simple and clear, thanks! |
As @andy-ms noted, this is a design limitation, since the compiler can not assert the relationship of the input to the output.. a functionally similar example that would work is: function f<T extends "a" | "b">(s: T): T {
switch (s) {
case "a": return s;
case "b": return s;
default: throw new Error();
}
} As for the OP, a related discussion can be found in #23132 |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.6.1
Code
Expected behavior:
So, When I use an generic interface, where the parameter is a
keyof
mapped type, inside a discriminated union (I know, I'm kinda using all advanced features in just one code; I swear it's real code), with--strictFunctionTypes
enabled, it should not report any error, both usingFnErr<D>
orFnOk<D>
.Actual behavior:
When
--strictFunctionTypes
is enabled, it reports onlyFnErr<D>
as structurally invalid.Is this an expected behavior? If so, why?
Thanks.
The text was updated successfully, but these errors were encountered: