-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Assignability check of index signature incorrectly reversed with non-trivial generics #35322
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
Interesting observation: I get the expected error if I reverse the assignment in my example to
Notice that this error only has 3 lines. It's missing the equivalent of the second line from the incorrect error in my original example. |
Looks to be a duplicate of #32311 |
@jack-williams Is there any recommendation for working around this limitation? Any hope for a fix in the future? |
Does this work? type Foo<T extends Record<keyof T, V>, V extends number | string = T[keyof T]> = {
readonly [n: number]: V;
}; EDIT: The following is shorter: type Foo<T extends Record<keyof T, number | string>> = Record<number, T[keyof T]>; |
That looks promising. I'll give it a try tonight and report back. |
@jack-williams Partial success with your workaround. In my oversimplified example, it seems to work across many TS versions (tested via the playground). But in my actual use case with much more complexity, I still get the same unexpected error in TS versions prior to 3.7. I'll continue to try to narrow down what's causing the error now. I'm developing a library published on NPM, so I'm trying to support as many versions back as possible. The good news is that the workaround doesn't introduce any new problems in earlier versions. Worst case scenario, my library will work as expected in TS 3.7+, but will require some brute force typecasting in earlier versions of TS if these kinds of assignments are used (luckily not a common use case). Thanks! |
We're thinking about this quite a bit and may special-case |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Uh oh!
There was an error while loading. Please reload this page.
TypeScript Version: 3.5.1 - 3.8.0-dev.20191123
Search Terms: index signature assignable reversed
Code
Expected behavior:
Foo<Record<string, 1 | 2 | 3>>
should be assignable toFoo<Record<string, number>>
because1 | 2 | 3
is assignable tonumber
.Actual behavior:
I get the following error. Notice how the direction of assignability in the error message seems to get incorrectly swapped starting on the second line of the error:
This error seems to have been introduced in version 3.5.1 (based on switching back to earlier versions of TS in the playground until there error disappeared).
Playground Link: http://www.typescriptlang.org/play/#code/C4TwDgpgBAYg9nAPAFShAHsCA7AJgZygCUIBjOAJ10QGsIQ4AzKZAGimwFcBbAIwgpQAPlHzAKAS2wBzAHyyoAXigBvAFBQoFCAENccbABsQUANrYAXBx78KAXSvJTdBs2R2A3GoC+XtbjJDHW0oQwhgaz4BeDgrGMQScipEMUkZdi4oinkvANIgkLCIzNsAGQksCh1DGLiEBLJKalSpaXYARmEoACYugGYctTUAemGoAFUAOQBRAA0ABWmAYWRpgBEoaaIiAHkiKAAJLemAQjUS6IQlSLKKgWqYvyG1UEgoACFglDRMHAIbgRdFoyBTKdSabR6AzGMyWAH2RxeXxDPIFaBFeGfChWLGIC7ZXKBYLo8Lw8qVapYnFfToiXoiAZ+UZQADKEm4YDCggwOg5YSgAAsdIRsHA0BQKJRBQIIOcbAIsdd8eT7oYsU8gA
Related Issues:
The text was updated successfully, but these errors were encountered: