-
Notifications
You must be signed in to change notification settings - Fork 13k
Unwrap substitution types early in getPropertyTypeForIndexType
#57113
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
Open
Andarist
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
Andarist:fix/avoid-redundant-substitution-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fdc0b7e
Avoid creating redundant substitution types in non-generic contexts
Andarist 6163188
Merge remote-tracking branch 'origin/main' into fix/avoid-redundant-s…
Andarist fec72b5
Unwrap substitution types early in `getPropertyTypeForIndexType`
Andarist c647e5d
add extra test case
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
34 changes: 34 additions & 0 deletions
34
tests/baselines/reference/nonGenericIndexedAccessInConditionalTrueBranch1.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,34 @@ | ||
//// [tests/cases/compiler/nonGenericIndexedAccessInConditionalTrueBranch1.ts] //// | ||
|
||
=== nonGenericIndexedAccessInConditionalTrueBranch1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/57109 | ||
|
||
type A = { | ||
>A : Symbol(A, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 0, 0)) | ||
|
||
k: symbol; | ||
>k : Symbol(k, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 2, 10)) | ||
|
||
}; | ||
|
||
type B = "k" extends keyof A ? A["k"] : never; | ||
>B : Symbol(B, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 4, 2)) | ||
>A : Symbol(A, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 0, 0)) | ||
>A : Symbol(A, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 0, 0)) | ||
|
||
type C = { | ||
>C : Symbol(C, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 6, 46)) | ||
|
||
k: symbol; | ||
>k : Symbol(k, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 8, 10)) | ||
|
||
other: boolean; | ||
>other : Symbol(other, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 9, 12)) | ||
|
||
}; | ||
|
||
type D = "k" extends keyof C ? C["k"] : never; | ||
>D : Symbol(D, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 11, 2)) | ||
>C : Symbol(C, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 6, 46)) | ||
>C : Symbol(C, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 6, 46)) | ||
|
30 changes: 30 additions & 0 deletions
30
tests/baselines/reference/nonGenericIndexedAccessInConditionalTrueBranch1.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,30 @@ | ||
//// [tests/cases/compiler/nonGenericIndexedAccessInConditionalTrueBranch1.ts] //// | ||
|
||
=== nonGenericIndexedAccessInConditionalTrueBranch1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/57109 | ||
|
||
type A = { | ||
>A : { k: symbol; } | ||
|
||
k: symbol; | ||
>k : symbol | ||
|
||
}; | ||
|
||
type B = "k" extends keyof A ? A["k"] : never; | ||
>B : symbol | ||
|
||
type C = { | ||
>C : { k: symbol; other: boolean; } | ||
|
||
k: symbol; | ||
>k : symbol | ||
|
||
other: boolean; | ||
>other : boolean | ||
|
||
}; | ||
|
||
type D = "k" extends keyof C ? C["k"] : never; | ||
>D : symbol | ||
|
17 changes: 17 additions & 0 deletions
17
tests/cases/compiler/nonGenericIndexedAccessInConditionalTrueBranch1.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,17 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
// https://github.com/microsoft/TypeScript/issues/57109 | ||
|
||
type A = { | ||
k: symbol; | ||
}; | ||
|
||
type B = "k" extends keyof A ? A["k"] : never; | ||
|
||
type C = { | ||
k: symbol; | ||
other: boolean; | ||
}; | ||
|
||
type D = "k" extends keyof C ? C["k"] : never; |
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.
alternatively, substitution types could get some improved handling in
getPropertyTypeForIndexType
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.
Yeah, I think I'd prefer
getPropertyNameFromIndex
+getPropertyTypeForIndexType
being updated to handle substitutions - the more places we recognize them as transparently their original type (if the substituted version isn't useful), the better, since it makes it easier on us to broaden the scope of how we use substitution types to support other features.As far as this approach goes...
instantiateTypeWorker
has similar logic to this - honestly, a lot of that logic should probably get moved into here, so it happens on initial construction and instantiation, rather than just instantiation.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.
I agree, the right approach is to erase them where needed. Omitting the substitution type just because the constraint is assignable isn't right and will definitely have adverse effects as the constraint may be a more specific type.
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.
I pushed out the requested change. I decided to use a similar approach of unwrapping the substation type early in
getPropertyTypeForIndexType
like it was done in #58292