Skip to content

String literal apparent types with specific member types #19846

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
@@ -6092,6 +6092,27 @@ namespace ts {
return type.resolvedApparentType || (type.resolvedApparentType = getTypeWithThisArgument(type, type));
}

function createStringLiteralApparentProperties(type: StringLiteralType): Type {
const propTable = createSymbolTable();
const possibleMembers: Type[] = [];
for (let i = 0; i < type.value.length; i++) {
const propName = "" + i as __String;
const prop = createSymbol(SymbolFlags.Property, propName);
prop.type = getLiteralType(type.value[i]);
propTable.set(propName, prop);
possibleMembers.push(prop.type);
}
if (strictNullChecks) {
possibleMembers.push(undefinedType);
}
copyEntries(resolveStructuredTypeMembers(globalStringType).members, propTable);
return createAnonymousType(type.symbol, propTable, emptyArray, emptyArray, /*stringIndexInfo*/ undefined, createIndexInfo(getUnionType(possibleMembers), /*isReadonly*/ true));
}

function getStringLiteralApparentProperties(type: StringLiteralType) {
return type.resolvedApparentType || (type.resolvedApparentType = createStringLiteralApparentProperties(type));
}

function getResolvedTypeParameterDefault(typeParameter: TypeParameter): Type | undefined {
if (!typeParameter.default) {
if (typeParameter.target) {
@@ -6147,6 +6168,7 @@ namespace ts {
function getApparentType(type: Type): Type {
const t = type.flags & TypeFlags.TypeVariable ? getBaseConstraintOfType(type) || emptyObjectType : type;
return t.flags & TypeFlags.Intersection ? getApparentTypeOfIntersectionType(<IntersectionType>t) :
t.flags & TypeFlags.StringLiteral ? getStringLiteralApparentProperties(t as StringLiteralType) :
t.flags & TypeFlags.StringLike ? globalStringType :
t.flags & TypeFlags.NumberLike ? globalNumberType :
t.flags & TypeFlags.BooleanLike ? globalBooleanType :
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
@@ -3347,6 +3347,7 @@ namespace ts {

export interface StringLiteralType extends LiteralType {
value: string;
/*@internal*/ resolvedApparentType?: Type; // Cached generated apparent properties
}

export interface NumberLiteralType extends LiteralType {
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//// [arrayLiteralContextualLiteralUnion.ts]
const x: 'a' | 'a'[] = ['a'];

//// [arrayLiteralContextualLiteralUnion.js]
var x = ['a'];
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/compiler/arrayLiteralContextualLiteralUnion.ts ===
const x: 'a' | 'a'[] = ['a'];
>x : Symbol(x, Decl(arrayLiteralContextualLiteralUnion.ts, 0, 5))

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/compiler/arrayLiteralContextualLiteralUnion.ts ===
const x: 'a' | 'a'[] = ['a'];
>x : "a" | "a"[]
>['a'] : ["a"]
>'a' : "a"

1 change: 1 addition & 0 deletions tests/baselines/reference/defaultIndexProps2.symbols
Original file line number Diff line number Diff line change
@@ -21,4 +21,5 @@ var o = {v:"Yo2"};
1[0];
var q = "s"[0];
>q : Symbol(q, Decl(defaultIndexProps2.ts, 13, 3))
>0 : Symbol(0)

4 changes: 2 additions & 2 deletions tests/baselines/reference/defaultIndexProps2.types
Original file line number Diff line number Diff line change
@@ -28,8 +28,8 @@ var o = {v:"Yo2"};
>0 : 0

var q = "s"[0];
>q : string
>"s"[0] : string
>q : "s"
>"s"[0] : "s"
>"s" : "s"
>0 : 0

10 changes: 5 additions & 5 deletions tests/baselines/reference/keyofAndIndexedAccessErrors.types
Original file line number Diff line number Diff line change
@@ -30,23 +30,23 @@ type T01 = keyof Object;
>Object : Object

type T02 = keyof keyof Object;
>T02 : "length" | "toString" | "valueOf" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
>T02 : "length" | "toString" | "valueOf" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
>Object : Object

type T03 = keyof keyof keyof Object;
>T03 : "length" | "toString" | "valueOf" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
>T03 : "length" | "toString" | "valueOf" | "0" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
>Object : Object

type T04 = keyof keyof keyof keyof Object;
>T04 : "length" | "toString" | "valueOf" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
>T04 : "length" | "toString" | "valueOf" | "0" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
>Object : Object

type T05 = keyof keyof keyof keyof keyof Object;
>T05 : "length" | "toString" | "valueOf" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
>T05 : "length" | "toString" | "valueOf" | "0" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
>Object : Object

type T06 = keyof keyof keyof keyof keyof keyof Object;
>T06 : "length" | "toString" | "valueOf" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
>T06 : "length" | "toString" | "valueOf" | "0" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
>Object : Object

type T10 = Shape["name"];
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/compiler/stringHasStringValuedNumericIndexer.ts ===
var str: string = ""[0];
>str : string
>""[0] : string
>""[0] : never
>"" : ""
>0 : 0

1 change: 1 addition & 0 deletions tests/cases/compiler/arrayLiteralContextualLiteralUnion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const x: 'a' | 'a'[] = ['a'];