-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Mapped generic type without indexer is assignable to indexer #32712
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
This is intentional (checker.ts line 14183): if (isGenericMappedType(source)) {
// A generic mapped type { [P in K]: T } is related to an index signature { [x: string]: U }
// if T is related to U.
return (kind === IndexKind.String && isRelatedTo(getTemplateTypeFromMappedType(source), targetInfo.type, reportErrors)) as any as Ternary;
} There's a consistency paradox here:
|
Hmm, I see the comment that says that's the intention, but it's unclear why it needs to be that way. If the behavior were to change such that |
The purpose of an indexer on the assignment target side is to say "Do all properties of this type satisfy this criteria?", where the criteria is "is assignable to this type". When a mapped type produces something that unconditionally satisfies the criteria, there's no logic by which to reject the assignment. |
Oh man, I could've sworn this behaved differently with a In light of that, I think what we'd actually want here is kind of the opposite of my original proposal: for consistency, it seems like the assignment I know there was a change in 3.5 to make it so Not a huge deal of course, but the inconsistency here between |
I'm sympathetic to the consistency argument, but |
Sure, though you don't always have control over which one is used (e.g. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 3.5.3, also 3.6.0-dev.20190804
Code
Expected behavior:
If
T
is not assignable to an indexer,Partial<T>
shouldn't be either.Playground Link:
Link
Related Issues:
#23080 (old bug where indexers were removed from mapped types)
#28798 (old bug with assignment in the other direction)
The text was updated successfully, but these errors were encountered: