Preserve input key style of computed properties in declaration emit (#55298)

This commit is contained in:
Mateusz Burzyński
2023-08-14 22:13:42 +02:00
committed by GitHub
parent 38553696e2
commit 16dab6d5d6
9 changed files with 144 additions and 14 deletions

View File

@@ -8082,7 +8082,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function isStringNamed(d: Declaration) {
const name = getNameOfDeclaration(d);
return !!name && isStringLiteral(name);
if (!name) {
return false;
}
if (isComputedPropertyName(name)) {
const type = checkExpression(name.expression);
return !!(type.flags & TypeFlags.StringLike);
}
return isStringLiteral(name);
}
function isSingleQuotedStringNamed(d: Declaration) {

View File

@@ -0,0 +1,40 @@
//// [tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts] ////
//// [declarationEmitPropertyNumericStringKey.ts]
// https://github.com/microsoft/TypeScript/issues/55292
const STATUS = {
["404"]: "not found",
} as const;
const hundredStr = "100";
const obj = { [hundredStr]: "foo" };
const hundredNum = 100;
const obj2 = { [hundredNum]: "bar" };
//// [declarationEmitPropertyNumericStringKey.js]
// https://github.com/microsoft/TypeScript/issues/55292
var _a, _b, _c;
var STATUS = (_a = {},
_a["404"] = "not found",
_a);
var hundredStr = "100";
var obj = (_b = {}, _b[hundredStr] = "foo", _b);
var hundredNum = 100;
var obj2 = (_c = {}, _c[hundredNum] = "bar", _c);
//// [declarationEmitPropertyNumericStringKey.d.ts]
declare const STATUS: {
readonly "404": "not found";
};
declare const hundredStr = "100";
declare const obj: {
"100": string;
};
declare const hundredNum = 100;
declare const obj2: {
100: string;
};

View File

@@ -0,0 +1,31 @@
//// [tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts] ////
=== declarationEmitPropertyNumericStringKey.ts ===
// https://github.com/microsoft/TypeScript/issues/55292
const STATUS = {
>STATUS : Symbol(STATUS, Decl(declarationEmitPropertyNumericStringKey.ts, 2, 5))
["404"]: "not found",
>["404"] : Symbol(["404"], Decl(declarationEmitPropertyNumericStringKey.ts, 2, 16))
>"404" : Symbol(["404"], Decl(declarationEmitPropertyNumericStringKey.ts, 2, 16))
} as const;
>const : Symbol(const)
const hundredStr = "100";
>hundredStr : Symbol(hundredStr, Decl(declarationEmitPropertyNumericStringKey.ts, 6, 5))
const obj = { [hundredStr]: "foo" };
>obj : Symbol(obj, Decl(declarationEmitPropertyNumericStringKey.ts, 7, 5))
>[hundredStr] : Symbol([hundredStr], Decl(declarationEmitPropertyNumericStringKey.ts, 7, 13))
>hundredStr : Symbol(hundredStr, Decl(declarationEmitPropertyNumericStringKey.ts, 6, 5))
const hundredNum = 100;
>hundredNum : Symbol(hundredNum, Decl(declarationEmitPropertyNumericStringKey.ts, 9, 5))
const obj2 = { [hundredNum]: "bar" };
>obj2 : Symbol(obj2, Decl(declarationEmitPropertyNumericStringKey.ts, 10, 5))
>[hundredNum] : Symbol([hundredNum], Decl(declarationEmitPropertyNumericStringKey.ts, 10, 14))
>hundredNum : Symbol(hundredNum, Decl(declarationEmitPropertyNumericStringKey.ts, 9, 5))

View File

@@ -0,0 +1,39 @@
//// [tests/cases/compiler/declarationEmitPropertyNumericStringKey.ts] ////
=== declarationEmitPropertyNumericStringKey.ts ===
// https://github.com/microsoft/TypeScript/issues/55292
const STATUS = {
>STATUS : { readonly "404": "not found"; }
>{ ["404"]: "not found",} as const : { readonly "404": "not found"; }
>{ ["404"]: "not found",} : { readonly "404": "not found"; }
["404"]: "not found",
>["404"] : "not found"
>"404" : "404"
>"not found" : "not found"
} as const;
const hundredStr = "100";
>hundredStr : "100"
>"100" : "100"
const obj = { [hundredStr]: "foo" };
>obj : { "100": string; }
>{ [hundredStr]: "foo" } : { "100": string; }
>[hundredStr] : string
>hundredStr : "100"
>"foo" : "foo"
const hundredNum = 100;
>hundredNum : 100
>100 : 100
const obj2 = { [hundredNum]: "bar" };
>obj2 : { 100: string; }
>{ [hundredNum]: "bar" } : { 100: string; }
>[hundredNum] : string
>hundredNum : 100
>"bar" : "bar"

View File

@@ -109,7 +109,7 @@ import { A } from './class';
export declare class B extends A {
getA(): {
a: string;
123123: string;
12312312312: string;
"123123": string;
"12312312312": string;
};
}

View File

@@ -34,7 +34,7 @@ export class A {
>getA : () => ITest
return {
>{ [TestEnum.Test1]: '123', [TestEnum.Test2]: '123', } : { 123123: string; 12312312312: string; }
>{ [TestEnum.Test1]: '123', [TestEnum.Test2]: '123', } : { "123123": string; "12312312312": string; }
[TestEnum.Test1]: '123',
>[TestEnum.Test1] : string
@@ -62,10 +62,10 @@ export class B extends A {
>A : A
getA() { // TS4053 error
>getA : () => { a: string; 123123: string; 12312312312: string; }
>getA : () => { a: string; "123123": string; "12312312312": string; }
return {
>{ ...super.getA(), a: '123', } : { a: string; 123123: string; 12312312312: string; }
>{ ...super.getA(), a: '123', } : { a: string; "123123": string; "12312312312": string; }
...super.getA(),
>super.getA() : import("class").ITest

View File

@@ -90,8 +90,8 @@ const t6 = {
}
const t7 = {
>t7 : { [-1]: number; }
>{ "-1": 1, ["-1"]: 0 // duplicate} : { [-1]: number; }
>t7 : { "-1": number; }
>{ "-1": 1, ["-1"]: 0 // duplicate} : { "-1": number; }
"-1": 1,
>"-1" : number

View File

@@ -2,8 +2,8 @@
=== literalsInComputedProperties1.ts ===
let x = {
>x : { 1: number; 2: number; "3": number; 4: number; }
>{ 1:1, [2]:1, "3":1, ["4"]:1} : { 1: number; 2: number; "3": number; 4: number; }
>x : { 1: number; 2: number; "3": number; "4": number; }
>{ 1:1, [2]:1, "3":1, ["4"]:1} : { 1: number; 2: number; "3": number; "4": number; }
1:1,
>1 : number
@@ -27,7 +27,7 @@ x[1].toExponential();
>x[1].toExponential() : string
>x[1].toExponential : (fractionDigits?: number) => string
>x[1] : number
>x : { 1: number; 2: number; "3": number; 4: number; }
>x : { 1: number; 2: number; "3": number; "4": number; }
>1 : 1
>toExponential : (fractionDigits?: number) => string
@@ -35,7 +35,7 @@ x[2].toExponential();
>x[2].toExponential() : string
>x[2].toExponential : (fractionDigits?: number) => string
>x[2] : number
>x : { 1: number; 2: number; "3": number; 4: number; }
>x : { 1: number; 2: number; "3": number; "4": number; }
>2 : 2
>toExponential : (fractionDigits?: number) => string
@@ -43,7 +43,7 @@ x[3].toExponential();
>x[3].toExponential() : string
>x[3].toExponential : (fractionDigits?: number) => string
>x[3] : number
>x : { 1: number; 2: number; "3": number; 4: number; }
>x : { 1: number; 2: number; "3": number; "4": number; }
>3 : 3
>toExponential : (fractionDigits?: number) => string
@@ -51,7 +51,7 @@ x[4].toExponential();
>x[4].toExponential() : string
>x[4].toExponential : (fractionDigits?: number) => string
>x[4] : number
>x : { 1: number; 2: number; "3": number; 4: number; }
>x : { 1: number; 2: number; "3": number; "4": number; }
>4 : 4
>toExponential : (fractionDigits?: number) => string

View File

@@ -0,0 +1,13 @@
// @declaration: true
// https://github.com/microsoft/TypeScript/issues/55292
const STATUS = {
["404"]: "not found",
} as const;
const hundredStr = "100";
const obj = { [hundredStr]: "foo" };
const hundredNum = 100;
const obj2 = { [hundredNum]: "bar" };