Mapped type and string index signature relations#17633
Conversation
sandersn
left a comment
There was a problem hiding this comment.
One small nit about the test.
| y = x; // Error | ||
| } | ||
|
|
||
| function f2<T, K extends string>(x: { [key: string]: T }, y: Record<string, T>) { |
There was a problem hiding this comment.
what's the point of this test? K isn't used.
There was a problem hiding this comment.
I guess it's to assert that the key type of Record doesn't matter, only the mapped type's template type. Seems self-evident to me, but maybe it's worth a test.
There was a problem hiding this comment.
To me it appears to be unused and unnecessary for the test. @ahejlsberg, is the type parameter K needed for this test?
There was a problem hiding this comment.
Yeah, no need to have that type parameter. Will fix.
| y = x; // Error | ||
| } | ||
|
|
||
| function f2<T, K extends string>(x: { [key: string]: T }, y: Record<string, T>) { |
There was a problem hiding this comment.
To me it appears to be unused and unnecessary for the test. @ahejlsberg, is the type parameter K needed for this test?
With this PR a mapped type
{ [P in K]: T }, whereKis generic, is related to a string index signature{ [key: string]: U }ifTis related toU. For example:Fixes #14548. Also fixes an unintended effect of #17382 that would allow any generic mapped type to be assignable to a string index signature (because generic mapped types have no manifest properties they were being mistaken for empty object types).