Skip to content

Commit 5ae4b5d

Browse files
authored
Properly preserve numeric string named properties in declaration files (#39658)
* Properly preserve names of properties with numeric literal strings * Accept new baselines
1 parent 3b22339 commit 5ae4b5d

21 files changed

+122
-122
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5575,15 +5575,14 @@ namespace ts {
55755575
}
55765576
}
55775577

5578+
function isStringNamed(d: Declaration) {
5579+
const name = getNameOfDeclaration(d);
5580+
return !!name && isStringLiteral(name);
5581+
}
5582+
55785583
function isSingleQuotedStringNamed(d: Declaration) {
55795584
const name = getNameOfDeclaration(d);
5580-
if (name && isStringLiteral(name) && (
5581-
name.singleQuote ||
5582-
(!nodeIsSynthesized(name) && startsWith(getTextOfNode(name, /*includeTrivia*/ false), "'"))
5583-
)) {
5584-
return true;
5585-
}
5586-
return false;
5585+
return !!(name && isStringLiteral(name) && (name.singleQuote || !nodeIsSynthesized(name) && startsWith(getTextOfNode(name, /*includeTrivia*/ false), "'")));
55875586
}
55885587

55895588
function getPropertyNameNodeForSymbol(symbol: Symbol, context: NodeBuilderContext) {
@@ -5596,7 +5595,8 @@ namespace ts {
55965595
return factory.createComputedPropertyName(factory.createPropertyAccessExpression(factory.createIdentifier("Symbol"), (symbol.escapedName as string).substr(3)));
55975596
}
55985597
const rawName = unescapeLeadingUnderscores(symbol.escapedName);
5599-
return createPropertyNameNodeForIdentifierOrLiteral(rawName, singleQuote);
5598+
const stringNamed = !!length(symbol.declarations) && every(symbol.declarations, isStringNamed);
5599+
return createPropertyNameNodeForIdentifierOrLiteral(rawName, stringNamed, singleQuote);
56005600
}
56015601

56025602
// See getNameForSymbolFromNameType for a stringy equivalent
@@ -5619,9 +5619,9 @@ namespace ts {
56195619
}
56205620
}
56215621

5622-
function createPropertyNameNodeForIdentifierOrLiteral(name: string, singleQuote?: boolean) {
5622+
function createPropertyNameNodeForIdentifierOrLiteral(name: string, stringNamed?: boolean, singleQuote?: boolean) {
56235623
return isIdentifierText(name, compilerOptions.target) ? factory.createIdentifier(name) :
5624-
isNumericLiteralName(name) && +name >= 0 ? factory.createNumericLiteral(+name) :
5624+
!stringNamed && isNumericLiteralName(name) && +name >= 0 ? factory.createNumericLiteral(+name) :
56255625
factory.createStringLiteral(name, !!singleQuote);
56265626
}
56275627

tests/baselines/reference/assignmentCompatWithObjectMembersStringNumericNames.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
1111
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(34,5): error TS2741: Property ''1.'' is missing in type 'S' but required in type '{ '1.': string; bar?: string; }'.
1212
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(35,5): error TS2741: Property ''1.'' is missing in type 'S2' but required in type '{ '1.': string; bar?: string; }'.
1313
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(36,5): error TS2741: Property ''1.'' is missing in type '{ '1.0': string; }' but required in type '{ '1.': string; bar?: string; }'.
14-
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(38,5): error TS2741: Property ''1.0'' is missing in type '{ 1: string; }' but required in type '{ '1.0': string; }'.
15-
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(39,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type '{ 1: string; }'.
14+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(38,5): error TS2741: Property ''1.0'' is missing in type '{ '1': string; }' but required in type '{ '1.0': string; }'.
15+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(39,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type '{ '1': string; }'.
1616
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(42,5): error TS2741: Property ''1.0'' is missing in type 'T' but required in type '{ '1.0': string; }'.
1717
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(65,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type 'S'.
1818
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts(71,5): error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type 'S2'.
@@ -108,11 +108,11 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
108108

109109
a2 = b2;
110110
~~
111-
!!! error TS2741: Property ''1.0'' is missing in type '{ 1: string; }' but required in type '{ '1.0': string; }'.
111+
!!! error TS2741: Property ''1.0'' is missing in type '{ '1': string; }' but required in type '{ '1.0': string; }'.
112112
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts:18:16: ''1.0'' is declared here.
113113
b2 = a2;
114114
~~
115-
!!! error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type '{ 1: string; }'.
115+
!!! error TS2741: Property ''1'' is missing in type '{ '1.0': string; }' but required in type '{ '1': string; }'.
116116
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersStringNumericNames.ts:19:16: ''1'' is declared here.
117117
a2 = b; // ok
118118
a2 = t2; // ok

tests/baselines/reference/assignmentCompatWithObjectMembersStringNumericNames.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ module JustStrings {
5050
>'' : ""
5151

5252
var b2 = { '1': '' };
53-
>b2 : { 1: string; }
54-
>{ '1': '' } : { 1: string; }
53+
>b2 : { '1': string; }
54+
>{ '1': '' } : { '1': string; }
5555
>'1' : string
5656
>'' : ""
5757

@@ -126,13 +126,13 @@ module JustStrings {
126126
>a2 : { '1.0': string; }
127127

128128
a2 = b2;
129-
>a2 = b2 : { 1: string; }
129+
>a2 = b2 : { '1': string; }
130130
>a2 : { '1.0': string; }
131-
>b2 : { 1: string; }
131+
>b2 : { '1': string; }
132132

133133
b2 = a2;
134134
>b2 = a2 : { '1.0': string; }
135-
>b2 : { 1: string; }
135+
>b2 : { '1': string; }
136136
>a2 : { '1.0': string; }
137137

138138
a2 = b; // ok

tests/baselines/reference/assignmentIndexedToPrimitives.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tests/cases/compiler/assignmentIndexedToPrimitives.ts(8,7): error TS2322: Type '
88
tests/cases/compiler/assignmentIndexedToPrimitives.ts(9,7): error TS2322: Type 'string[]' is not assignable to type '"01"'.
99
tests/cases/compiler/assignmentIndexedToPrimitives.ts(11,7): error TS2322: Type '{ 0: number; }' is not assignable to type 'number'.
1010
tests/cases/compiler/assignmentIndexedToPrimitives.ts(13,7): error TS2322: Type '{ 0: number; }' is not assignable to type 'string'.
11-
tests/cases/compiler/assignmentIndexedToPrimitives.ts(14,7): error TS2322: Type '{ 0: number; }' is not assignable to type 'string'.
11+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(14,7): error TS2322: Type '{ "0": number; }' is not assignable to type 'string'.
1212
tests/cases/compiler/assignmentIndexedToPrimitives.ts(15,7): error TS2322: Type '{ 0: string; }' is not assignable to type 'string'.
1313

1414

@@ -48,7 +48,7 @@ tests/cases/compiler/assignmentIndexedToPrimitives.ts(15,7): error TS2322: Type
4848
!!! error TS2322: Type '{ 0: number; }' is not assignable to type 'string'.
4949
const so2: string = { "0": 1 };
5050
~~~
51-
!!! error TS2322: Type '{ 0: number; }' is not assignable to type 'string'.
51+
!!! error TS2322: Type '{ "0": number; }' is not assignable to type 'string'.
5252
const so3: string = { 0: "1" };
5353
~~~
5454
!!! error TS2322: Type '{ 0: string; }' is not assignable to type 'string'.

tests/baselines/reference/assignmentIndexedToPrimitives.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const so1: string = { 0: 1 };
5656

5757
const so2: string = { "0": 1 };
5858
>so2 : string
59-
>{ "0": 1 } : { 0: number; }
59+
>{ "0": 1 } : { "0": number; }
6060
>"0" : number
6161
>1 : 1
6262

tests/baselines/reference/deepKeysIndexing.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type keys2workaround<O extends DeepObject, K1 extends keyof O> = Extract<
3333

3434
interface Foo extends DeepObject {
3535
a: {
36-
>a : { 1: 123; 2: string; 3: boolean; }
36+
>a : { "1": 123; "2": string; "3": boolean; }
3737

3838
"1": 123;
3939
>"1" : 123

tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES5.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
>({ "1": "one", "2": "two" } as { [key: string]: string })[x] : string
66
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
77
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
8-
>{ "1": "one", "2": "two" } : { 1: string; 2: string; }
8+
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
99
>"1" : string
1010
>"one" : "one"
1111
>"2" : string
@@ -19,7 +19,7 @@
1919
>({ "1": "one", "2": "two" } as { [key: string]: string }).x : string
2020
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
2121
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
22-
>{ "1": "one", "2": "two" } : { 1: string; 2: string; }
22+
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
2323
>"1" : string
2424
>"one" : "one"
2525
>"2" : string

tests/baselines/reference/emitAccessExpressionOfCastedObjectLiteralExpressionInArrowFunctionES6.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
>({ "1": "one", "2": "two" } as { [key: string]: string })[x] : string
66
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
77
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
8-
>{ "1": "one", "2": "two" } : { 1: string; 2: string; }
8+
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
99
>"1" : string
1010
>"one" : "one"
1111
>"2" : string
@@ -19,7 +19,7 @@
1919
>({ "1": "one", "2": "two" } as { [key: string]: string }).x : string
2020
>({ "1": "one", "2": "two" } as { [key: string]: string }) : { [key: string]: string; }
2121
>{ "1": "one", "2": "two" } as { [key: string]: string } : { [key: string]: string; }
22-
>{ "1": "one", "2": "two" } : { 1: string; 2: string; }
22+
>{ "1": "one", "2": "two" } : { "1": string; "2": string; }
2323
>"1" : string
2424
>"one" : "one"
2525
>"2" : string

tests/baselines/reference/enumKeysQuotedAsObjectPropertiesInDeclarationEmit.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ export declare enum MouseButton {
5151
}
5252
export declare const DOMMouseButton: {
5353
'-1': MouseButton;
54-
0: MouseButton;
55-
1: MouseButton;
56-
2: MouseButton;
57-
3: MouseButton;
58-
4: MouseButton;
54+
"0": MouseButton;
55+
"1": MouseButton;
56+
"2": MouseButton;
57+
"3": MouseButton;
58+
"4": MouseButton;
5959
};

tests/baselines/reference/enumKeysQuotedAsObjectPropertiesInDeclarationEmit.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export enum MouseButton {
2828
}
2929

3030
export const DOMMouseButton = {
31-
>DOMMouseButton : { '-1': MouseButton; 0: MouseButton; 1: MouseButton; 2: MouseButton; 3: MouseButton; 4: MouseButton; }
32-
>{ '-1': MouseButton.NO_BUTTON, "0": MouseButton.LEFT_BUTTON, "1": MouseButton.MIDDLE_BUTTON, "2": MouseButton.RIGHT_BUTTON, "3": MouseButton.XBUTTON1_BUTTON, "4": MouseButton.XBUTTON2_BUTTON,} : { '-1': MouseButton; 0: MouseButton; 1: MouseButton; 2: MouseButton; 3: MouseButton; 4: MouseButton; }
31+
>DOMMouseButton : { '-1': MouseButton; "0": MouseButton; "1": MouseButton; "2": MouseButton; "3": MouseButton; "4": MouseButton; }
32+
>{ '-1': MouseButton.NO_BUTTON, "0": MouseButton.LEFT_BUTTON, "1": MouseButton.MIDDLE_BUTTON, "2": MouseButton.RIGHT_BUTTON, "3": MouseButton.XBUTTON1_BUTTON, "4": MouseButton.XBUTTON2_BUTTON,} : { '-1': MouseButton; "0": MouseButton; "1": MouseButton; "2": MouseButton; "3": MouseButton; "4": MouseButton; }
3333

3434
'-1': MouseButton.NO_BUTTON,
3535
>'-1' : MouseButton

tests/baselines/reference/inferringAnyFunctionType1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/compiler/inferringAnyFunctionType1.ts ===
22
function f<T extends { "0": (p1: number) => number }>(p: T): T {
3-
>f : <T extends { 0: (p1: number) => number; }>(p: T) => T
3+
>f : <T extends { "0": (p1: number) => number; }>(p: T) => T
44
>"0" : (p1: number) => number
55
>p1 : number
66
>p : T
@@ -12,7 +12,7 @@ function f<T extends { "0": (p1: number) => number }>(p: T): T {
1212
var v = f([x => x]);
1313
>v : [(x: number) => number]
1414
>f([x => x]) : [(x: number) => number]
15-
>f : <T extends { 0: (p1: number) => number; }>(p: T) => T
15+
>f : <T extends { "0": (p1: number) => number; }>(p: T) => T
1616
>[x => x] : [(x: number) => number]
1717
>x => x : (x: number) => number
1818
>x : number

tests/baselines/reference/literalsInComputedProperties1.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
=== tests/cases/compiler/literalsInComputedProperties1.ts ===
22
let x = {
3-
>x : { 1: number; 2: number; 3: number; 4: number; }
4-
>{ 1:1, [2]:1, "3":1, ["4"]:1} : { 1: number; 2: number; 3: number; 4: number; }
3+
>x : { 1: number; 2: number; "3": number; 4: number; }
4+
>{ 1:1, [2]:1, "3":1, ["4"]:1} : { 1: number; 2: number; "3": number; 4: number; }
55

66
1:1,
77
>1 : number
@@ -25,31 +25,31 @@ x[1].toExponential();
2525
>x[1].toExponential() : string
2626
>x[1].toExponential : (fractionDigits?: number) => string
2727
>x[1] : number
28-
>x : { 1: number; 2: number; 3: number; 4: number; }
28+
>x : { 1: number; 2: number; "3": number; 4: number; }
2929
>1 : 1
3030
>toExponential : (fractionDigits?: number) => string
3131

3232
x[2].toExponential();
3333
>x[2].toExponential() : string
3434
>x[2].toExponential : (fractionDigits?: number) => string
3535
>x[2] : number
36-
>x : { 1: number; 2: number; 3: number; 4: number; }
36+
>x : { 1: number; 2: number; "3": number; 4: number; }
3737
>2 : 2
3838
>toExponential : (fractionDigits?: number) => string
3939

4040
x[3].toExponential();
4141
>x[3].toExponential() : string
4242
>x[3].toExponential : (fractionDigits?: number) => string
4343
>x[3] : number
44-
>x : { 1: number; 2: number; 3: number; 4: number; }
44+
>x : { 1: number; 2: number; "3": number; 4: number; }
4545
>3 : 3
4646
>toExponential : (fractionDigits?: number) => string
4747

4848
x[4].toExponential();
4949
>x[4].toExponential() : string
5050
>x[4].toExponential : (fractionDigits?: number) => string
5151
>x[4] : number
52-
>x : { 1: number; 2: number; 3: number; 4: number; }
52+
>x : { 1: number; 2: number; "3": number; 4: number; }
5353
>4 : 4
5454
>toExponential : (fractionDigits?: number) => string
5555

tests/baselines/reference/multipleNumericIndexers.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var b: {
3939
>x : number
4040

4141
} = { 1: '', "2": '' }
42-
>{ 1: '', "2": '' } : { 1: string; 2: string; }
42+
>{ 1: '', "2": '' } : { 1: string; "2": string; }
4343
>1 : string
4444
>'' : ""
4545
>"2" : string

tests/baselines/reference/noImplicitAnyIndexing.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ interface MyMap<T> {
8282

8383
var m: MyMap<number> = {
8484
>m : MyMap<number>
85-
>{ "0": 0, "1": 1, "2": 2, "Okay that's enough for today.": NaN} : { 0: number; 1: number; 2: number; "Okay that's enough for today.": number; }
85+
>{ "0": 0, "1": 1, "2": 2, "Okay that's enough for today.": NaN} : { "0": number; "1": number; "2": number; "Okay that's enough for today.": number; }
8686

8787
"0": 0,
8888
>"0" : number

tests/baselines/reference/noImplicitAnyIndexingSuppressed.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ interface MyMap<T> {
8181

8282
var m: MyMap<number> = {
8383
>m : MyMap<number>
84-
>{ "0": 0, "1": 1, "2": 2, "Okay that's enough for today.": NaN} : { 0: number; 1: number; 2: number; "Okay that's enough for today.": number; }
84+
>{ "0": 0, "1": 1, "2": 2, "Okay that's enough for today.": NaN} : { "0": number; "1": number; "2": number; "Okay that's enough for today.": number; }
8585

8686
"0": 0,
8787
>"0" : number

0 commit comments

Comments
 (0)