Skip to content

Commit b8e0009

Browse files
authored
Set syntheticLiteralTypeOrigin on synthetic undefined-type members (#22216)
* Have getNameOfSymbolAsWritten quote nonidentifier nonnumeric symbols all the time * Revert checker changes * Reuse synthetic origin to indicate that derived declaration name may need to be quoted
1 parent 1a43ad0 commit b8e0009

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11150,6 +11150,10 @@ namespace ts {
1115011150
}
1115111151
const result = createSymbol(SymbolFlags.Property | SymbolFlags.Optional, name);
1115211152
result.type = undefinedType;
11153+
const associatedKeyType = getLiteralType(unescapeLeadingUnderscores(name));
11154+
if (associatedKeyType.flags & TypeFlags.StringLiteral) {
11155+
result.syntheticLiteralTypeOrigin = associatedKeyType as StringLiteralType;
11156+
}
1115311157
undefinedProperties.set(name, result);
1115411158
return result;
1115511159
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [inferredNonidentifierTypesGetQuotes.ts]
2+
var x = [{ "a-b": "string" }, {}];
3+
4+
var y = [{ ["a-b"]: "string" }, {}];
5+
6+
//// [inferredNonidentifierTypesGetQuotes.js]
7+
var x = [{ "a-b": "string" }, {}];
8+
var y = [(_a = {}, _a["a-b"] = "string", _a), {}];
9+
var _a;
10+
11+
12+
//// [inferredNonidentifierTypesGetQuotes.d.ts]
13+
declare var x: ({
14+
"a-b": string;
15+
} | {
16+
"a-b"?: undefined;
17+
})[];
18+
declare var y: ({
19+
["a-b"]: string;
20+
} | {
21+
"a-b"?: undefined;
22+
})[];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/inferredNonidentifierTypesGetQuotes.ts ===
2+
var x = [{ "a-b": "string" }, {}];
3+
>x : Symbol(x, Decl(inferredNonidentifierTypesGetQuotes.ts, 0, 3))
4+
>"a-b" : Symbol("a-b", Decl(inferredNonidentifierTypesGetQuotes.ts, 0, 10))
5+
6+
var y = [{ ["a-b"]: "string" }, {}];
7+
>y : Symbol(y, Decl(inferredNonidentifierTypesGetQuotes.ts, 2, 3))
8+
>["a-b"] : Symbol(["a-b"], Decl(inferredNonidentifierTypesGetQuotes.ts, 2, 10))
9+
>"a-b" : Symbol(["a-b"], Decl(inferredNonidentifierTypesGetQuotes.ts, 2, 10))
10+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/inferredNonidentifierTypesGetQuotes.ts ===
2+
var x = [{ "a-b": "string" }, {}];
3+
>x : ({ "a-b": string; } | { "a-b"?: undefined; })[]
4+
>[{ "a-b": "string" }, {}] : ({ "a-b": string; } | {})[]
5+
>{ "a-b": "string" } : { "a-b": string; }
6+
>"a-b" : string
7+
>"string" : "string"
8+
>{} : {}
9+
10+
var y = [{ ["a-b"]: "string" }, {}];
11+
>y : ({ ["a-b"]: string; } | { "a-b"?: undefined; })[]
12+
>[{ ["a-b"]: "string" }, {}] : ({ ["a-b"]: string; } | {})[]
13+
>{ ["a-b"]: "string" } : { ["a-b"]: string; }
14+
>["a-b"] : string
15+
>"a-b" : "a-b"
16+
>"string" : "string"
17+
>{} : {}
18+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @declaration: true
2+
var x = [{ "a-b": "string" }, {}];
3+
4+
var y = [{ ["a-b"]: "string" }, {}];

0 commit comments

Comments
 (0)