-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Values of string literal type are ignored in generic function #31732
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
Comments
@ahejlsberg I think this would be covered by the reverse return type inference we were discussing yesterday? |
@RyanCavanaugh No, generic return type inference doesn't cover this. When the result of a function call is the source of an assignment and we have a known type on the target side we'll perform generic return type inference. However, in this example, the result of the first function call is the function value for the second function call, so there is no contextual typing. |
This is working as intended. In const enhanced1 = decorate({ one: 42, two: "three" } as const)(MyCompnent)
const enhanced2 = decorate({ one: 42, two: "wut" } as const)(MyCompnent)
const enhanced3 = decorate({ one: 42, two: 111 } as const)(MyCompnent) The first call then works and the second two fail as expected. Now, the reason the first two calls aren't errors in non-strict mode is that we check parameter types bivariantly when relating function types (which is unsound). We default to non-strict for reasons of backwards compatibility, but we definitely recommend using |
Also, #31243 is a suggestion to perform generic return type inference for functions returning functions that are immediately invoked. That would cause us to contextually type the argument to |
String literal types are ignored and treated as just "string" when working with generics.
Also the line ("Test 3") which is expectedly fails to compile the error message contains this:
but actual problem in line "Test 2" seems to ignore this fact
TypeScript Version:
Search Terms:
Code
Expected behavior:
the following piece of code:
should not be compiled since
MyComponent
does not allow value "wut" for field "two".Actual behavior:
Playground Link:
playground
Related Issues:
The text was updated successfully, but these errors were encountered: