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
Fix inference to indexed access type containing substitution type #31141
Merged
ahejlsberg
merged 4 commits into
master
from
fixInferenceToIndexedAccessWithSubstitution
May 1, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
30 changes: 30 additions & 0 deletions
30
tests/baselines/reference/substitutionTypesInIndexedAccessTypes.js
This file contains 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 @@ | ||
//// [substitutionTypesInIndexedAccessTypes.ts] | ||
// Repro from #31086 | ||
|
||
type UserArgs = { | ||
select?: boolean | ||
}; | ||
|
||
type Subset<T, U> = { [key in keyof T]: key extends keyof U ? T[key] : never }; | ||
|
||
declare function withBoundary<T extends UserArgs>(args?: Subset<T, UserArgs>): T; | ||
declare function withoutBoundary<T extends UserArgs>(args?: T): T; | ||
|
||
const boundaryResult = withBoundary({ | ||
select: true, | ||
}); | ||
|
||
const withoutBoundaryResult = withoutBoundary({ | ||
select: true, | ||
}); | ||
|
||
|
||
//// [substitutionTypesInIndexedAccessTypes.js] | ||
"use strict"; | ||
// Repro from #31086 | ||
var boundaryResult = withBoundary({ | ||
select: true | ||
}); | ||
var withoutBoundaryResult = withoutBoundary({ | ||
select: true | ||
}); |
58 changes: 58 additions & 0 deletions
58
tests/baselines/reference/substitutionTypesInIndexedAccessTypes.symbols
This file contains 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,58 @@ | ||
=== tests/cases/compiler/substitutionTypesInIndexedAccessTypes.ts === | ||
// Repro from #31086 | ||
|
||
type UserArgs = { | ||
>UserArgs : Symbol(UserArgs, Decl(substitutionTypesInIndexedAccessTypes.ts, 0, 0)) | ||
|
||
select?: boolean | ||
>select : Symbol(select, Decl(substitutionTypesInIndexedAccessTypes.ts, 2, 17)) | ||
|
||
}; | ||
|
||
type Subset<T, U> = { [key in keyof T]: key extends keyof U ? T[key] : never }; | ||
>Subset : Symbol(Subset, Decl(substitutionTypesInIndexedAccessTypes.ts, 4, 2)) | ||
>T : Symbol(T, Decl(substitutionTypesInIndexedAccessTypes.ts, 6, 12)) | ||
>U : Symbol(U, Decl(substitutionTypesInIndexedAccessTypes.ts, 6, 14)) | ||
>key : Symbol(key, Decl(substitutionTypesInIndexedAccessTypes.ts, 6, 23)) | ||
>T : Symbol(T, Decl(substitutionTypesInIndexedAccessTypes.ts, 6, 12)) | ||
>key : Symbol(key, Decl(substitutionTypesInIndexedAccessTypes.ts, 6, 23)) | ||
>U : Symbol(U, Decl(substitutionTypesInIndexedAccessTypes.ts, 6, 14)) | ||
>T : Symbol(T, Decl(substitutionTypesInIndexedAccessTypes.ts, 6, 12)) | ||
>key : Symbol(key, Decl(substitutionTypesInIndexedAccessTypes.ts, 6, 23)) | ||
|
||
declare function withBoundary<T extends UserArgs>(args?: Subset<T, UserArgs>): T; | ||
>withBoundary : Symbol(withBoundary, Decl(substitutionTypesInIndexedAccessTypes.ts, 6, 79)) | ||
>T : Symbol(T, Decl(substitutionTypesInIndexedAccessTypes.ts, 8, 30)) | ||
>UserArgs : Symbol(UserArgs, Decl(substitutionTypesInIndexedAccessTypes.ts, 0, 0)) | ||
>args : Symbol(args, Decl(substitutionTypesInIndexedAccessTypes.ts, 8, 50)) | ||
>Subset : Symbol(Subset, Decl(substitutionTypesInIndexedAccessTypes.ts, 4, 2)) | ||
>T : Symbol(T, Decl(substitutionTypesInIndexedAccessTypes.ts, 8, 30)) | ||
>UserArgs : Symbol(UserArgs, Decl(substitutionTypesInIndexedAccessTypes.ts, 0, 0)) | ||
>T : Symbol(T, Decl(substitutionTypesInIndexedAccessTypes.ts, 8, 30)) | ||
|
||
declare function withoutBoundary<T extends UserArgs>(args?: T): T; | ||
>withoutBoundary : Symbol(withoutBoundary, Decl(substitutionTypesInIndexedAccessTypes.ts, 8, 81)) | ||
>T : Symbol(T, Decl(substitutionTypesInIndexedAccessTypes.ts, 9, 33)) | ||
>UserArgs : Symbol(UserArgs, Decl(substitutionTypesInIndexedAccessTypes.ts, 0, 0)) | ||
>args : Symbol(args, Decl(substitutionTypesInIndexedAccessTypes.ts, 9, 53)) | ||
>T : Symbol(T, Decl(substitutionTypesInIndexedAccessTypes.ts, 9, 33)) | ||
>T : Symbol(T, Decl(substitutionTypesInIndexedAccessTypes.ts, 9, 33)) | ||
|
||
const boundaryResult = withBoundary({ | ||
>boundaryResult : Symbol(boundaryResult, Decl(substitutionTypesInIndexedAccessTypes.ts, 11, 5)) | ||
>withBoundary : Symbol(withBoundary, Decl(substitutionTypesInIndexedAccessTypes.ts, 6, 79)) | ||
|
||
select: true, | ||
>select : Symbol(select, Decl(substitutionTypesInIndexedAccessTypes.ts, 11, 37)) | ||
|
||
}); | ||
|
||
const withoutBoundaryResult = withoutBoundary({ | ||
>withoutBoundaryResult : Symbol(withoutBoundaryResult, Decl(substitutionTypesInIndexedAccessTypes.ts, 15, 5)) | ||
>withoutBoundary : Symbol(withoutBoundary, Decl(substitutionTypesInIndexedAccessTypes.ts, 8, 81)) | ||
|
||
select: true, | ||
>select : Symbol(select, Decl(substitutionTypesInIndexedAccessTypes.ts, 15, 47)) | ||
|
||
}); | ||
|
46 changes: 46 additions & 0 deletions
46
tests/baselines/reference/substitutionTypesInIndexedAccessTypes.types
This file contains 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,46 @@ | ||
=== tests/cases/compiler/substitutionTypesInIndexedAccessTypes.ts === | ||
// Repro from #31086 | ||
|
||
type UserArgs = { | ||
>UserArgs : UserArgs | ||
|
||
select?: boolean | ||
>select : boolean | undefined | ||
|
||
}; | ||
|
||
type Subset<T, U> = { [key in keyof T]: key extends keyof U ? T[key] : never }; | ||
>Subset : Subset<T, U> | ||
|
||
declare function withBoundary<T extends UserArgs>(args?: Subset<T, UserArgs>): T; | ||
>withBoundary : <T extends UserArgs>(args?: Subset<T, UserArgs> | undefined) => T | ||
>args : Subset<T, UserArgs> | undefined | ||
|
||
declare function withoutBoundary<T extends UserArgs>(args?: T): T; | ||
>withoutBoundary : <T extends UserArgs>(args?: T | undefined) => T | ||
>args : T | undefined | ||
|
||
const boundaryResult = withBoundary({ | ||
>boundaryResult : { select: true; } | ||
>withBoundary({ select: true,}) : { select: true; } | ||
>withBoundary : <T extends UserArgs>(args?: Subset<T, UserArgs> | undefined) => T | ||
>{ select: true,} : { select: true; } | ||
|
||
select: true, | ||
>select : true | ||
>true : true | ||
|
||
}); | ||
|
||
const withoutBoundaryResult = withoutBoundary({ | ||
>withoutBoundaryResult : { select: true; } | ||
>withoutBoundary({ select: true,}) : { select: true; } | ||
>withoutBoundary : <T extends UserArgs>(args?: T | undefined) => T | ||
>{ select: true,} : { select: true; } | ||
|
||
select: true, | ||
>select : true | ||
>true : true | ||
|
||
}); | ||
|
20 changes: 20 additions & 0 deletions
20
tests/cases/compiler/substitutionTypesInIndexedAccessTypes.ts
This file contains 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,20 @@ | ||
// @strict: true | ||
|
||
// Repro from #31086 | ||
|
||
type UserArgs = { | ||
select?: boolean | ||
}; | ||
|
||
type Subset<T, U> = { [key in keyof T]: key extends keyof U ? T[key] : never }; | ||
|
||
declare function withBoundary<T extends UserArgs>(args?: Subset<T, UserArgs>): T; | ||
declare function withoutBoundary<T extends UserArgs>(args?: T): T; | ||
|
||
const boundaryResult = withBoundary({ | ||
select: true, | ||
}); | ||
|
||
const withoutBoundaryResult = withoutBoundary({ | ||
select: true, | ||
}); |
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.
What about a
Substitute<T[Substitute<K>]>
? Substitutes can in theory nest arbitrarily, provided they keep instantiating to type variables.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've moved the logic to
getActualTypeVariable
and made it recurse appropriately.