Skip to content

Commit 16dab6d

Browse files
authored
Preserve input key style of computed properties in declaration emit (#55298)
1 parent 3855369 commit 16dab6d

9 files changed

+144
-14
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8082,7 +8082,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
80828082

80838083
function isStringNamed(d: Declaration) {
80848084
const name = getNameOfDeclaration(d);
8085-
return !!name && isStringLiteral(name);
8085+
if (!name) {
8086+
return false;
8087+
}
8088+
if (isComputedPropertyName(name)) {
8089+
const type = checkExpression(name.expression);
8090+
return !!(type.flags & TypeFlags.StringLike);
8091+
}
8092+
return isStringLiteral(name);
80868093
}
80878094

80888095
function isSingleQuotedStringNamed(d: Declaration) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts] ////
2+
3+
//// [declarationEmitPropertyNumericStringKey.ts]
4+
// https://github.com/microsoft/TypeScript/issues/55292
5+
6+
const STATUS = {
7+
["404"]: "not found",
8+
} as const;
9+
10+
const hundredStr = "100";
11+
const obj = { [hundredStr]: "foo" };
12+
13+
const hundredNum = 100;
14+
const obj2 = { [hundredNum]: "bar" };
15+
16+
17+
//// [declarationEmitPropertyNumericStringKey.js]
18+
// https://github.com/microsoft/TypeScript/issues/55292
19+
var _a, _b, _c;
20+
var STATUS = (_a = {},
21+
_a["404"] = "not found",
22+
_a);
23+
var hundredStr = "100";
24+
var obj = (_b = {}, _b[hundredStr] = "foo", _b);
25+
var hundredNum = 100;
26+
var obj2 = (_c = {}, _c[hundredNum] = "bar", _c);
27+
28+
29+
//// [declarationEmitPropertyNumericStringKey.d.ts]
30+
declare const STATUS: {
31+
readonly "404": "not found";
32+
};
33+
declare const hundredStr = "100";
34+
declare const obj: {
35+
"100": string;
36+
};
37+
declare const hundredNum = 100;
38+
declare const obj2: {
39+
100: string;
40+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts] ////
2+
3+
=== declarationEmitPropertyNumericStringKey.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55292
5+
6+
const STATUS = {
7+
>STATUS : Symbol(STATUS, Decl(declarationEmitPropertyNumericStringKey.ts, 2, 5))
8+
9+
["404"]: "not found",
10+
>["404"] : Symbol(["404"], Decl(declarationEmitPropertyNumericStringKey.ts, 2, 16))
11+
>"404" : Symbol(["404"], Decl(declarationEmitPropertyNumericStringKey.ts, 2, 16))
12+
13+
} as const;
14+
>const : Symbol(const)
15+
16+
const hundredStr = "100";
17+
>hundredStr : Symbol(hundredStr, Decl(declarationEmitPropertyNumericStringKey.ts, 6, 5))
18+
19+
const obj = { [hundredStr]: "foo" };
20+
>obj : Symbol(obj, Decl(declarationEmitPropertyNumericStringKey.ts, 7, 5))
21+
>[hundredStr] : Symbol([hundredStr], Decl(declarationEmitPropertyNumericStringKey.ts, 7, 13))
22+
>hundredStr : Symbol(hundredStr, Decl(declarationEmitPropertyNumericStringKey.ts, 6, 5))
23+
24+
const hundredNum = 100;
25+
>hundredNum : Symbol(hundredNum, Decl(declarationEmitPropertyNumericStringKey.ts, 9, 5))
26+
27+
const obj2 = { [hundredNum]: "bar" };
28+
>obj2 : Symbol(obj2, Decl(declarationEmitPropertyNumericStringKey.ts, 10, 5))
29+
>[hundredNum] : Symbol([hundredNum], Decl(declarationEmitPropertyNumericStringKey.ts, 10, 14))
30+
>hundredNum : Symbol(hundredNum, Decl(declarationEmitPropertyNumericStringKey.ts, 9, 5))
31+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//// [tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts] ////
2+
3+
=== declarationEmitPropertyNumericStringKey.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55292
5+
6+
const STATUS = {
7+
>STATUS : { readonly "404": "not found"; }
8+
>{ ["404"]: "not found",} as const : { readonly "404": "not found"; }
9+
>{ ["404"]: "not found",} : { readonly "404": "not found"; }
10+
11+
["404"]: "not found",
12+
>["404"] : "not found"
13+
>"404" : "404"
14+
>"not found" : "not found"
15+
16+
} as const;
17+
18+
const hundredStr = "100";
19+
>hundredStr : "100"
20+
>"100" : "100"
21+
22+
const obj = { [hundredStr]: "foo" };
23+
>obj : { "100": string; }
24+
>{ [hundredStr]: "foo" } : { "100": string; }
25+
>[hundredStr] : string
26+
>hundredStr : "100"
27+
>"foo" : "foo"
28+
29+
const hundredNum = 100;
30+
>hundredNum : 100
31+
>100 : 100
32+
33+
const obj2 = { [hundredNum]: "bar" };
34+
>obj2 : { 100: string; }
35+
>{ [hundredNum]: "bar" } : { 100: string; }
36+
>[hundredNum] : string
37+
>hundredNum : 100
38+
>"bar" : "bar"
39+

tests/baselines/reference/declarationEmitStringEnumUsedInNonlocalSpread.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ import { A } from './class';
109109
export declare class B extends A {
110110
getA(): {
111111
a: string;
112-
123123: string;
113-
12312312312: string;
112+
"123123": string;
113+
"12312312312": string;
114114
};
115115
}

tests/baselines/reference/declarationEmitStringEnumUsedInNonlocalSpread.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class A {
3434
>getA : () => ITest
3535

3636
return {
37-
>{ [TestEnum.Test1]: '123', [TestEnum.Test2]: '123', } : { 123123: string; 12312312312: string; }
37+
>{ [TestEnum.Test1]: '123', [TestEnum.Test2]: '123', } : { "123123": string; "12312312312": string; }
3838

3939
[TestEnum.Test1]: '123',
4040
>[TestEnum.Test1] : string
@@ -62,10 +62,10 @@ export class B extends A {
6262
>A : A
6363

6464
getA() { // TS4053 error
65-
>getA : () => { a: string; 123123: string; 12312312312: string; }
65+
>getA : () => { a: string; "123123": string; "12312312312": string; }
6666

6767
return {
68-
>{ ...super.getA(), a: '123', } : { a: string; 123123: string; 12312312312: string; }
68+
>{ ...super.getA(), a: '123', } : { a: string; "123123": string; "12312312312": string; }
6969

7070
...super.getA(),
7171
>super.getA() : import("class").ITest

tests/baselines/reference/duplicateObjectLiteralProperty_computedName1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ const t6 = {
9090
}
9191

9292
const t7 = {
93-
>t7 : { [-1]: number; }
94-
>{ "-1": 1, ["-1"]: 0 // duplicate} : { [-1]: number; }
93+
>t7 : { "-1": number; }
94+
>{ "-1": 1, ["-1"]: 0 // duplicate} : { "-1": number; }
9595

9696
"-1": 1,
9797
>"-1" : number

tests/baselines/reference/literalsInComputedProperties1.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
=== literalsInComputedProperties1.ts ===
44
let x = {
5-
>x : { 1: number; 2: number; "3": number; 4: number; }
6-
>{ 1:1, [2]:1, "3":1, ["4"]:1} : { 1: number; 2: number; "3": number; 4: number; }
5+
>x : { 1: number; 2: number; "3": number; "4": number; }
6+
>{ 1:1, [2]:1, "3":1, ["4"]:1} : { 1: number; 2: number; "3": number; "4": number; }
77

88
1:1,
99
>1 : number
@@ -27,31 +27,31 @@ x[1].toExponential();
2727
>x[1].toExponential() : string
2828
>x[1].toExponential : (fractionDigits?: number) => string
2929
>x[1] : number
30-
>x : { 1: number; 2: number; "3": number; 4: number; }
30+
>x : { 1: number; 2: number; "3": number; "4": number; }
3131
>1 : 1
3232
>toExponential : (fractionDigits?: number) => string
3333

3434
x[2].toExponential();
3535
>x[2].toExponential() : string
3636
>x[2].toExponential : (fractionDigits?: number) => string
3737
>x[2] : number
38-
>x : { 1: number; 2: number; "3": number; 4: number; }
38+
>x : { 1: number; 2: number; "3": number; "4": number; }
3939
>2 : 2
4040
>toExponential : (fractionDigits?: number) => string
4141

4242
x[3].toExponential();
4343
>x[3].toExponential() : string
4444
>x[3].toExponential : (fractionDigits?: number) => string
4545
>x[3] : number
46-
>x : { 1: number; 2: number; "3": number; 4: number; }
46+
>x : { 1: number; 2: number; "3": number; "4": number; }
4747
>3 : 3
4848
>toExponential : (fractionDigits?: number) => string
4949

5050
x[4].toExponential();
5151
>x[4].toExponential() : string
5252
>x[4].toExponential : (fractionDigits?: number) => string
5353
>x[4] : number
54-
>x : { 1: number; 2: number; "3": number; 4: number; }
54+
>x : { 1: number; 2: number; "3": number; "4": number; }
5555
>4 : 4
5656
>toExponential : (fractionDigits?: number) => string
5757

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @declaration: true
2+
3+
// https://github.com/microsoft/TypeScript/issues/55292
4+
5+
const STATUS = {
6+
["404"]: "not found",
7+
} as const;
8+
9+
const hundredStr = "100";
10+
const obj = { [hundredStr]: "foo" };
11+
12+
const hundredNum = 100;
13+
const obj2 = { [hundredNum]: "bar" };

0 commit comments

Comments
 (0)