-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow indexing generics with unique symbols #22339
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
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
33 changes: 33 additions & 0 deletions
33
tests/baselines/reference/lateBoundConstraintTypeChecksCorrectly.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,33 @@ | ||
//// [lateBoundConstraintTypeChecksCorrectly.ts] | ||
declare const fooProp: unique symbol; | ||
declare const barProp: unique symbol; | ||
|
||
type BothProps = typeof fooProp | typeof barProp; | ||
|
||
export interface Foo<T> { | ||
[fooProp]: T; | ||
[barProp]: string; | ||
} | ||
|
||
function f<T extends Foo<number>>(x: T) { | ||
const abc = x[fooProp]; // expected: 'T[typeof fooProp]' | ||
|
||
/** | ||
* Expected: no error | ||
*/ | ||
const def: T[typeof fooProp] = x[fooProp]; | ||
const def2: T[typeof barProp] = x[barProp]; | ||
} | ||
|
||
|
||
//// [lateBoundConstraintTypeChecksCorrectly.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
function f(x) { | ||
var abc = x[fooProp]; // expected: 'T[typeof fooProp]' | ||
/** | ||
* Expected: no error | ||
*/ | ||
var def = x[fooProp]; | ||
var def2 = x[barProp]; | ||
} |
56 changes: 56 additions & 0 deletions
56
tests/baselines/reference/lateBoundConstraintTypeChecksCorrectly.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,56 @@ | ||
=== tests/cases/compiler/lateBoundConstraintTypeChecksCorrectly.ts === | ||
declare const fooProp: unique symbol; | ||
>fooProp : Symbol(fooProp, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 0, 13)) | ||
|
||
declare const barProp: unique symbol; | ||
>barProp : Symbol(barProp, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 1, 13)) | ||
|
||
type BothProps = typeof fooProp | typeof barProp; | ||
>BothProps : Symbol(BothProps, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 1, 37)) | ||
>fooProp : Symbol(fooProp, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 0, 13)) | ||
>barProp : Symbol(barProp, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 1, 13)) | ||
|
||
export interface Foo<T> { | ||
>Foo : Symbol(Foo, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 3, 49)) | ||
>T : Symbol(T, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 5, 21)) | ||
|
||
[fooProp]: T; | ||
>[fooProp] : Symbol(Foo[fooProp], Decl(lateBoundConstraintTypeChecksCorrectly.ts, 5, 25)) | ||
>fooProp : Symbol(fooProp, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 0, 13)) | ||
>T : Symbol(T, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 5, 21)) | ||
|
||
[barProp]: string; | ||
>[barProp] : Symbol(Foo[barProp], Decl(lateBoundConstraintTypeChecksCorrectly.ts, 6, 15)) | ||
>barProp : Symbol(barProp, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 1, 13)) | ||
} | ||
|
||
function f<T extends Foo<number>>(x: T) { | ||
>f : Symbol(f, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 8, 1)) | ||
>T : Symbol(T, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 10, 11)) | ||
>Foo : Symbol(Foo, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 3, 49)) | ||
>x : Symbol(x, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 10, 34)) | ||
>T : Symbol(T, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 10, 11)) | ||
|
||
const abc = x[fooProp]; // expected: 'T[typeof fooProp]' | ||
>abc : Symbol(abc, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 11, 9)) | ||
>x : Symbol(x, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 10, 34)) | ||
>fooProp : Symbol(fooProp, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 0, 13)) | ||
|
||
/** | ||
* Expected: no error | ||
*/ | ||
const def: T[typeof fooProp] = x[fooProp]; | ||
>def : Symbol(def, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 16, 9)) | ||
>T : Symbol(T, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 10, 11)) | ||
>fooProp : Symbol(fooProp, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 0, 13)) | ||
>x : Symbol(x, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 10, 34)) | ||
>fooProp : Symbol(fooProp, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 0, 13)) | ||
|
||
const def2: T[typeof barProp] = x[barProp]; | ||
>def2 : Symbol(def2, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 17, 9)) | ||
>T : Symbol(T, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 10, 11)) | ||
>barProp : Symbol(barProp, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 1, 13)) | ||
>x : Symbol(x, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 10, 34)) | ||
>barProp : Symbol(barProp, Decl(lateBoundConstraintTypeChecksCorrectly.ts, 1, 13)) | ||
} | ||
|
59 changes: 59 additions & 0 deletions
59
tests/baselines/reference/lateBoundConstraintTypeChecksCorrectly.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,59 @@ | ||
=== tests/cases/compiler/lateBoundConstraintTypeChecksCorrectly.ts === | ||
declare const fooProp: unique symbol; | ||
>fooProp : unique symbol | ||
|
||
declare const barProp: unique symbol; | ||
>barProp : unique symbol | ||
|
||
type BothProps = typeof fooProp | typeof barProp; | ||
>BothProps : unique symbol | unique symbol | ||
>fooProp : unique symbol | ||
>barProp : unique symbol | ||
|
||
export interface Foo<T> { | ||
>Foo : Foo<T> | ||
>T : T | ||
|
||
[fooProp]: T; | ||
>[fooProp] : T | ||
>fooProp : unique symbol | ||
>T : T | ||
|
||
[barProp]: string; | ||
>[barProp] : string | ||
>barProp : unique symbol | ||
} | ||
|
||
function f<T extends Foo<number>>(x: T) { | ||
>f : <T extends Foo<number>>(x: T) => void | ||
>T : T | ||
>Foo : Foo<T> | ||
>x : T | ||
>T : T | ||
|
||
const abc = x[fooProp]; // expected: 'T[typeof fooProp]' | ||
>abc : number | ||
>x[fooProp] : number | ||
>x : T | ||
>fooProp : unique symbol | ||
|
||
/** | ||
* Expected: no error | ||
*/ | ||
const def: T[typeof fooProp] = x[fooProp]; | ||
>def : T[unique symbol] | ||
>T : T | ||
>fooProp : unique symbol | ||
>x[fooProp] : number | ||
>x : T | ||
>fooProp : unique symbol | ||
|
||
const def2: T[typeof barProp] = x[barProp]; | ||
>def2 : T[unique symbol] | ||
>T : T | ||
>barProp : unique symbol | ||
>x[barProp] : string | ||
>x : T | ||
>barProp : unique symbol | ||
} | ||
|
19 changes: 19 additions & 0 deletions
19
tests/cases/compiler/lateBoundConstraintTypeChecksCorrectly.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,19 @@ | ||
declare const fooProp: unique symbol; | ||
declare const barProp: unique symbol; | ||
|
||
type BothProps = typeof fooProp | typeof barProp; | ||
|
||
export interface Foo<T> { | ||
[fooProp]: T; | ||
[barProp]: string; | ||
} | ||
|
||
function f<T extends Foo<number>>(x: T) { | ||
const abc = x[fooProp]; // expected: 'T[typeof fooProp]' | ||
|
||
/** | ||
* Expected: no error | ||
*/ | ||
const def: T[typeof fooProp] = x[fooProp]; | ||
const def2: T[typeof barProp] = x[barProp]; | ||
} |
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.
Since this is a
StringLiteral
, does this need to be changed? Is it just for consistency?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.
Mostly, yeah. The function below is used up above in the same body and is the correct one to use for future support of symbols as mapped type keys.