-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Avoid rewriting homomorphic mapped types with homomorphic instantiations #53215
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
Merged
weswigham
merged 6 commits into
microsoft:main
from
Andarist:fix/inline-mapped-type-rewriting-in-declarations
Mar 21, 2023
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a927933
Avoid rewriting mapped types that are already inline
Andarist f562093
tweak isHomomorphicMappedTypeWithNonHomomorphicInstantiation
Andarist 5d51cf7
Avoid rewriting mapped types without a target
Andarist c9c4682
Merge remote-tracking branch 'origin/main' into fix/inline-mapped-typ…
Andarist 587d9e7
implemented suggested changes
Andarist 4b6afba
Merge remote-tracking branch 'origin/main' into fix/inline-mapped-typ…
Andarist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/baselines/reference/mappedTypeGenericInstantiationPreservesInlineForm.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| //// [mappedTypeGenericInstantiationPreservesInlineForm.ts] | ||
| // repro from #53109 | ||
|
|
||
| export const test1 = <T = Record<string, never>>(schema: { | ||
| [K in keyof Required<T>]: T[K]; | ||
| }) => {} | ||
|
|
||
| export function test2<T = Record<string, never>>(schema: { | ||
| [K in keyof Required<T>]: T[K]; | ||
| }) {}; | ||
|
|
||
|
|
||
|
|
||
|
|
||
| //// [mappedTypeGenericInstantiationPreservesInlineForm.d.ts] | ||
| export declare const test1: <T = Record<string, never>>(schema: { [K in keyof Required<T>]: T[K]; }) => void; | ||
| export declare function test2<T = Record<string, never>>(schema: { | ||
| [K in keyof Required<T>]: T[K]; | ||
| }): void; |
33 changes: 33 additions & 0 deletions
33
tests/baselines/reference/mappedTypeGenericInstantiationPreservesInlineForm.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| === tests/cases/compiler/mappedTypeGenericInstantiationPreservesInlineForm.ts === | ||
| // repro from #53109 | ||
|
|
||
| export const test1 = <T = Record<string, never>>(schema: { | ||
| >test1 : Symbol(test1, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 2, 12)) | ||
| >T : Symbol(T, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 2, 22)) | ||
| >Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
| >schema : Symbol(schema, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 2, 49)) | ||
|
|
||
| [K in keyof Required<T>]: T[K]; | ||
| >K : Symbol(K, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 3, 5)) | ||
| >Required : Symbol(Required, Decl(lib.es5.d.ts, --, --)) | ||
| >T : Symbol(T, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 2, 22)) | ||
| >T : Symbol(T, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 2, 22)) | ||
| >K : Symbol(K, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 3, 5)) | ||
|
|
||
| }) => {} | ||
|
|
||
| export function test2<T = Record<string, never>>(schema: { | ||
| >test2 : Symbol(test2, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 4, 8)) | ||
| >T : Symbol(T, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 6, 22)) | ||
| >Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
| >schema : Symbol(schema, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 6, 49)) | ||
|
|
||
| [K in keyof Required<T>]: T[K]; | ||
| >K : Symbol(K, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 7, 5)) | ||
| >Required : Symbol(Required, Decl(lib.es5.d.ts, --, --)) | ||
| >T : Symbol(T, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 6, 22)) | ||
| >T : Symbol(T, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 6, 22)) | ||
| >K : Symbol(K, Decl(mappedTypeGenericInstantiationPreservesInlineForm.ts, 7, 5)) | ||
|
|
||
| }) {}; | ||
|
|
18 changes: 18 additions & 0 deletions
18
tests/baselines/reference/mappedTypeGenericInstantiationPreservesInlineForm.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| === tests/cases/compiler/mappedTypeGenericInstantiationPreservesInlineForm.ts === | ||
| // repro from #53109 | ||
|
|
||
| export const test1 = <T = Record<string, never>>(schema: { | ||
| >test1 : <T = Record<string, never>>(schema: { [K in keyof Required<T>]: T[K]; }) => void | ||
| ><T = Record<string, never>>(schema: { [K in keyof Required<T>]: T[K];}) => {} : <T = Record<string, never>>(schema: { [K in keyof Required<T>]: T[K]; }) => void | ||
| >schema : { [K in keyof Required<T>]: T[K]; } | ||
|
|
||
| [K in keyof Required<T>]: T[K]; | ||
| }) => {} | ||
|
|
||
| export function test2<T = Record<string, never>>(schema: { | ||
| >test2 : <T = Record<string, never>>(schema: { [K in keyof Required<T>]: T[K]; }) => void | ||
| >schema : { [K in keyof Required<T>]: T[K]; } | ||
|
|
||
| [K in keyof Required<T>]: T[K]; | ||
| }) {}; | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/cases/compiler/mappedTypeGenericInstantiationPreservesInlineForm.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // @strict: true | ||
| // @declaration: true | ||
| // @emitDeclarationOnly: true | ||
|
|
||
| // repro from #53109 | ||
|
|
||
| export const test1 = <T = Record<string, never>>(schema: { | ||
| [K in keyof Required<T>]: T[K]; | ||
| }) => {} | ||
|
|
||
| export function test2<T = Record<string, never>>(schema: { | ||
| [K in keyof Required<T>]: T[K]; | ||
| }) {}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't have anything to do with the alias symbol as by the time we're trying to print with this function, the alias symbol has already been discarded as an option for printing the mapped type. Instead, what we care about is if the type will be picked up as a homomorphic type variable (or not). Type parameters always are, so were a conservative initial choice - it's generally safe to wrap anything else which is still recognized as a homomorphic type variable up and present it as a type parameter, too - all you get is unnecessarily verbose printback sometimes. Instead of checking the alias symbol here (which seems pretty unrelated), you want to check, rather than if the modifiers type is still a type parameter, if an index on the modifiers type still produces a homomorphic type variable. In this case, an index of a mapped type just returns the inner mapped type's constraint type, which can then be unwrapped, so it is recognized as homomorphic. So, instead of the simple, conservative
!(getModifiersTypeFromMappedType(type).flags & TypeFlags.TypeParameter)check, you want something to more closely mirrorgetHomomorphicTypeVariable's conditions likeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I applied the suggested change but it reverted the change to the emit of this thing (TS playground):
Does this one have to be rewritten? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not, since, in that case, I don't think
PartialPropertiesis homomorphic, so the wrapping mapped type shouldn't be either. Right now we're usingisMappedTypeWithKeyofConstraintDeclarationas a proxy for was-this-declared-as-homomorphic - we probably need to swap that out for something a bit more accurate, too. Potentially the same check we're doing ontypebut ontype.target(and negated)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, it's not actually homomorphic -
ends up with type
so we just need to tighten up that side of the check, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This particular mapped type doesn't have a
.targetproperty - I might have misunderstood your recommendation for the proposed changes but for now, I've chosen to just avoid rewriting in case of!type.target. I think this is correct (it yields the expected result for this case and the rest of the test suite) but maybe I'm missing some further details.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, that's perfect - if the mapped type doesn't have a
.target, then leaving it as-is is always correct - it's still it's declared form, uninstantiated.