-
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 1 commit
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3725,8 +3725,8 @@ namespace ts { | |
return "(Anonymous function)"; | ||
} | ||
} | ||
if ((symbol as TransientSymbol).syntheticLiteralTypeOrigin) { | ||
const stringValue = (symbol as TransientSymbol).syntheticLiteralTypeOrigin.value; | ||
if ((symbol as TransientSymbol).nameType && (symbol as TransientSymbol).nameType.flags & TypeFlags.StringLiteral) { | ||
const stringValue = ((symbol as TransientSymbol).nameType as StringLiteralType).value; | ||
if (!isIdentifierText(stringValue, compilerOptions.target)) { | ||
return `"${escapeString(stringValue, CharacterCodes.doubleQuote)}"`; | ||
} | ||
|
@@ -5460,6 +5460,12 @@ namespace ts { | |
lateSymbol = createSymbol(SymbolFlags.None, memberName, CheckFlags.Late); | ||
} | ||
|
||
const symbolLinks = getSymbolLinks(lateSymbol); | ||
if (!symbolLinks.nameType) { | ||
// Retain link to name type so that it can be reused later | ||
symbolLinks.nameType = type; | ||
} | ||
|
||
addDeclarationToLateBoundSymbol(lateSymbol, decl, symbolFlags); | ||
if (lateSymbol.parent) { | ||
Debug.assert(lateSymbol.parent === parent, "Existing symbol parent should match new one"); | ||
|
@@ -5948,7 +5954,7 @@ namespace ts { | |
// If the current iteration type constituent is a string literal type, create a property. | ||
// Otherwise, for type string create a string index signature. | ||
if (t.flags & TypeFlags.StringLiteral) { | ||
const propName = escapeLeadingUnderscores((<StringLiteralType>t).value); | ||
const propName = getLateBoundNameFromType(t as LiteralType | UniqueESSymbolType); | ||
const modifiersProp = getPropertyOfType(modifiersType, propName); | ||
const isOptional = !!(templateModifiers & MappedTypeModifiers.IncludeOptional || | ||
!(templateModifiers & MappedTypeModifiers.ExcludeOptional) && modifiersProp && modifiersProp.flags & SymbolFlags.Optional); | ||
|
@@ -5965,7 +5971,7 @@ namespace ts { | |
prop.syntheticOrigin = propertySymbol; | ||
prop.declarations = propertySymbol.declarations; | ||
} | ||
prop.syntheticLiteralTypeOrigin = t as StringLiteralType; | ||
prop.nameType = t; | ||
members.set(propName, prop); | ||
} | ||
else if (t.flags & (TypeFlags.Any | TypeFlags.String)) { | ||
|
@@ -7999,9 +8005,18 @@ namespace ts { | |
} | ||
|
||
function getLiteralTypeFromPropertyName(prop: Symbol) { | ||
return getDeclarationModifierFlagsFromSymbol(prop) & ModifierFlags.NonPublicAccessibilityModifier || isKnownSymbol(prop) ? | ||
neverType : | ||
getLiteralType(symbolName(prop)); | ||
const links = getSymbolLinks(prop); | ||
if (!links.nameType) { | ||
if (links.target && links.target.escapedName === prop.escapedName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't have to check names here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to debug assertion. |
||
links.nameType = getLiteralTypeFromPropertyName(links.target); | ||
} | ||
else { | ||
links.nameType = getDeclarationModifierFlagsFromSymbol(prop) & ModifierFlags.NonPublicAccessibilityModifier || isKnownSymbol(prop) ? | ||
neverType : | ||
getLiteralType(symbolName(prop)); | ||
} | ||
} | ||
return links.nameType; | ||
} | ||
|
||
function getLiteralTypeFromPropertyNames(type: Type) { | ||
|
@@ -11225,7 +11240,7 @@ namespace ts { | |
result.type = undefinedType; | ||
const associatedKeyType = getLiteralType(unescapeLeadingUnderscores(name)); | ||
if (associatedKeyType.flags & TypeFlags.StringLiteral) { | ||
result.syntheticLiteralTypeOrigin = associatedKeyType as StringLiteralType; | ||
result.nameType = associatedKeyType; | ||
} | ||
undefinedProperties.set(name, result); | ||
return result; | ||
|
@@ -14998,10 +15013,15 @@ namespace ts { | |
typeFlags |= type.flags; | ||
|
||
const nameType = hasLateBindableName(memberDecl) ? checkComputedPropertyName(memberDecl.name) : undefined; | ||
const prop = nameType && isTypeUsableAsLateBoundName(nameType) | ||
? createSymbol(SymbolFlags.Property | member.flags, getLateBoundNameFromType(nameType), CheckFlags.Late) | ||
const hasLateBoundName = nameType && isTypeUsableAsLateBoundName(nameType); | ||
const prop = hasLateBoundName | ||
? createSymbol(SymbolFlags.Property | member.flags, getLateBoundNameFromType(nameType as LiteralType | UniqueESSymbolType), CheckFlags.Late) | ||
: createSymbol(SymbolFlags.Property | member.flags, literalName || member.escapedName); | ||
|
||
if (hasLateBoundName) { | ||
prop.nameType = nameType; | ||
} | ||
|
||
if (inDestructuringPattern) { | ||
// If object literal is an assignment pattern and if the assignment pattern specifies a default value | ||
// for the property, make the property optional. | ||
|
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.