From fe6890ad539e8394b77f9f16b137ad8fb4788f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Thu, 6 Jun 2024 11:38:24 +0200 Subject: [PATCH 1/6] Fixed declaration emit crash related to enum entity name expressions --- src/compiler/checker.ts | 14 ++++- ...eclarationEmitComputedPropertyNameEnum1.js | 34 +++++++++++++ ...ationEmitComputedPropertyNameEnum1.symbols | 29 +++++++++++ ...arationEmitComputedPropertyNameEnum1.types | 51 +++++++++++++++++++ ...eclarationEmitComputedPropertyNameEnum1.ts | 16 ++++++ 5 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.js create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.symbols create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.types create mode 100644 tests/cases/compiler/declarationEmitComputedPropertyNameEnum1.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d56a2de17879b..a939a6d96f594 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8856,8 +8856,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { else { const type = getWidenedType(getRegularTypeOfExpression(node.expression)); const computedPropertyNameType = typeToTypeNodeHelper(type, context); - Debug.assertNode(computedPropertyNameType, isLiteralTypeNode); - const literal = computedPropertyNameType.literal; + let literal; + if (isLiteralTypeNode(computedPropertyNameType)) { + literal = computedPropertyNameType.literal; + } + else { + const evaluated = evaluateEntityNameExpression(node.expression); + const literalNode = typeof evaluated.value === 'string' ? factory.createStringLiteral(evaluated.value, /*isSingleQuote*/ undefined) : + typeof evaluated.value === 'number' ? factory.createNumericLiteral(evaluated.value, /*numericLiteralFlags*/ 0) : + undefined; + Debug.assert(literalNode); + literal = literalNode; + } if (literal.kind === SyntaxKind.StringLiteral && isIdentifierText(literal.text, getEmitScriptTarget(compilerOptions))) { return factory.createIdentifier(literal.text); } diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.js b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.js new file mode 100644 index 0000000000000..162a3c3064c32 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.js @@ -0,0 +1,34 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameEnum1.ts] //// + +//// [type.ts] +export enum Enum { + A = "a", + B = "b" +} + +export type Type = { x?: { [Enum.A]: 0 } }; + +//// [index.ts] +import { type Type } from "./type"; + +export const foo = { ...({} as Type) }; + + + + +//// [type.d.ts] +export declare enum Enum { + A = "a", + B = "b" +} +export type Type = { + x?: { + [Enum.A]: 0; + }; +}; +//// [index.d.ts] +export declare const foo: { + x?: { + a: 0; + }; +}; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.symbols b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.symbols new file mode 100644 index 0000000000000..77b89dd3cbd43 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.symbols @@ -0,0 +1,29 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameEnum1.ts] //// + +=== type.ts === +export enum Enum { +>Enum : Symbol(Enum, Decl(type.ts, 0, 0)) + + A = "a", +>A : Symbol(Enum.A, Decl(type.ts, 0, 18)) + + B = "b" +>B : Symbol(Enum.B, Decl(type.ts, 1, 10)) +} + +export type Type = { x?: { [Enum.A]: 0 } }; +>Type : Symbol(Type, Decl(type.ts, 3, 1)) +>x : Symbol(x, Decl(type.ts, 5, 20)) +>[Enum.A] : Symbol([Enum.A], Decl(type.ts, 5, 26)) +>Enum.A : Symbol(Enum.A, Decl(type.ts, 0, 18)) +>Enum : Symbol(Enum, Decl(type.ts, 0, 0)) +>A : Symbol(Enum.A, Decl(type.ts, 0, 18)) + +=== index.ts === +import { type Type } from "./type"; +>Type : Symbol(Type, Decl(index.ts, 0, 8)) + +export const foo = { ...({} as Type) }; +>foo : Symbol(foo, Decl(index.ts, 2, 12)) +>Type : Symbol(Type, Decl(index.ts, 0, 8)) + diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.types b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.types new file mode 100644 index 0000000000000..9e356be01688d --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum1.types @@ -0,0 +1,51 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameEnum1.ts] //// + +=== type.ts === +export enum Enum { +>Enum : Enum +> : ^^^^ + + A = "a", +>A : Enum.A +> : ^^^^^^ +>"a" : "a" +> : ^^^ + + B = "b" +>B : Enum.B +> : ^^^^^^ +>"b" : "b" +> : ^^^ +} + +export type Type = { x?: { [Enum.A]: 0 } }; +>Type : Type +> : ^^^^ +>x : { a: 0; } | undefined +> : ^^^^^ ^^^^^^^^^^^^^^^ +>[Enum.A] : 0 +> : ^ +>Enum.A : Enum.A +> : ^^^^^^ +>Enum : typeof Enum +> : ^^^^^^^^^^^ +>A : Enum.A +> : ^^^^^^ + +=== index.ts === +import { type Type } from "./type"; +>Type : any +> : ^^^ + +export const foo = { ...({} as Type) }; +>foo : { x?: { a: 0; }; } +> : ^^^^^^ ^^^ +>{ ...({} as Type) } : { x?: { a: 0; }; } +> : ^^^^^^ ^^^ +>({} as Type) : Type +> : ^^^^ +>{} as Type : Type +> : ^^^^ +>{} : {} +> : ^^ + diff --git a/tests/cases/compiler/declarationEmitComputedPropertyNameEnum1.ts b/tests/cases/compiler/declarationEmitComputedPropertyNameEnum1.ts new file mode 100644 index 0000000000000..b17a555c9842f --- /dev/null +++ b/tests/cases/compiler/declarationEmitComputedPropertyNameEnum1.ts @@ -0,0 +1,16 @@ +// @strict: true +// @declaration: true +// @emitDeclarationOnly: true + +// @filename: type.ts +export enum Enum { + A = "a", + B = "b" +} + +export type Type = { x?: { [Enum.A]: 0 } }; + +// @filename: index.ts +import { type Type } from "./type"; + +export const foo = { ...({} as Type) }; From 27d94c52297a67f4a873f787b284e47fe4fbe17b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Thu, 6 Jun 2024 11:42:39 +0200 Subject: [PATCH 2/6] fmt --- src/compiler/checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a939a6d96f594..a2f6c9b0addf4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8862,8 +8862,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } else { const evaluated = evaluateEntityNameExpression(node.expression); - const literalNode = typeof evaluated.value === 'string' ? factory.createStringLiteral(evaluated.value, /*isSingleQuote*/ undefined) : - typeof evaluated.value === 'number' ? factory.createNumericLiteral(evaluated.value, /*numericLiteralFlags*/ 0) : + const literalNode = typeof evaluated.value === "string" ? factory.createStringLiteral(evaluated.value, /*isSingleQuote*/ undefined) : + typeof evaluated.value === "number" ? factory.createNumericLiteral(evaluated.value, /*numericLiteralFlags*/ 0) : undefined; Debug.assert(literalNode); literal = literalNode; From f119176fdf85c049e4d2d626c6fc812ba3db2b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Thu, 6 Jun 2024 23:29:50 +0200 Subject: [PATCH 3/6] reuse the existing node when evaluation fails --- src/compiler/checker.ts | 4 +- ...onEmitComputedPropertyNameEnum2.errors.txt | 16 ++++++ ...eclarationEmitComputedPropertyNameEnum2.js | 23 ++++++++ ...ationEmitComputedPropertyNameEnum2.symbols | 16 ++++++ ...arationEmitComputedPropertyNameEnum2.types | 34 ++++++++++++ ...onEmitComputedPropertyNameEnum3.errors.txt | 22 ++++++++ ...eclarationEmitComputedPropertyNameEnum3.js | 33 +++++++++++ ...ationEmitComputedPropertyNameEnum3.symbols | 32 +++++++++++ ...arationEmitComputedPropertyNameEnum3.types | 55 +++++++++++++++++++ ...EmitComputedPropertyNameSymbol1.errors.txt | 16 ++++++ ...larationEmitComputedPropertyNameSymbol1.js | 31 +++++++++++ ...ionEmitComputedPropertyNameSymbol1.symbols | 25 +++++++++ ...ationEmitComputedPropertyNameSymbol1.types | 42 ++++++++++++++ ...EmitComputedPropertyNameSymbol2.errors.txt | 19 +++++++ ...larationEmitComputedPropertyNameSymbol2.js | 26 +++++++++ ...ionEmitComputedPropertyNameSymbol2.symbols | 25 +++++++++ ...ationEmitComputedPropertyNameSymbol2.types | 42 ++++++++++++++ ...eclarationEmitComputedPropertyNameEnum2.ts | 11 ++++ ...eclarationEmitComputedPropertyNameEnum3.ts | 17 ++++++ ...larationEmitComputedPropertyNameSymbol1.ts | 14 +++++ ...larationEmitComputedPropertyNameSymbol2.ts | 14 +++++ 21 files changed, 516 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.errors.txt create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.js create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.symbols create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.types create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.errors.txt create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.js create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.symbols create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.types create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types create mode 100644 tests/cases/compiler/declarationEmitComputedPropertyNameEnum2.ts create mode 100644 tests/cases/compiler/declarationEmitComputedPropertyNameEnum3.ts create mode 100644 tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts create mode 100644 tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a2f6c9b0addf4..19207b4295bcd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8865,7 +8865,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { const literalNode = typeof evaluated.value === "string" ? factory.createStringLiteral(evaluated.value, /*isSingleQuote*/ undefined) : typeof evaluated.value === "number" ? factory.createNumericLiteral(evaluated.value, /*numericLiteralFlags*/ 0) : undefined; - Debug.assert(literalNode); + if (!literalNode) { + return node; + } literal = literalNode; } if (literal.kind === SyntaxKind.StringLiteral && isIdentifierText(literal.text, getEmitScriptTarget(compilerOptions))) { diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.errors.txt b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.errors.txt new file mode 100644 index 0000000000000..bef0d0f6357bb --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.errors.txt @@ -0,0 +1,16 @@ +type.ts(1,28): error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. +type.ts(1,29): error TS2304: Cannot find name 'Enum'. + + +==== type.ts (2 errors) ==== + export type Type = { x?: { [Enum.A]: 0 } }; + ~~~~~~~~ +!!! error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. + ~~~~ +!!! error TS2304: Cannot find name 'Enum'. + +==== index.ts (0 errors) ==== + import { type Type } from "./type"; + + export const foo = { ...({} as Type) }; + \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.js b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.js new file mode 100644 index 0000000000000..c77d5998d8705 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.js @@ -0,0 +1,23 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameEnum2.ts] //// + +//// [type.ts] +export type Type = { x?: { [Enum.A]: 0 } }; + +//// [index.ts] +import { type Type } from "./type"; + +export const foo = { ...({} as Type) }; + + + + +//// [type.d.ts] +export type Type = { + x?: {}; +}; +//// [index.d.ts] +export declare const foo: { + x?: { + [Enum.A]: 0; + }; +}; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.symbols b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.symbols new file mode 100644 index 0000000000000..82eb16b58d52e --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.symbols @@ -0,0 +1,16 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameEnum2.ts] //// + +=== type.ts === +export type Type = { x?: { [Enum.A]: 0 } }; +>Type : Symbol(Type, Decl(type.ts, 0, 0)) +>x : Symbol(x, Decl(type.ts, 0, 20)) +>[Enum.A] : Symbol([Enum.A], Decl(type.ts, 0, 26)) + +=== index.ts === +import { type Type } from "./type"; +>Type : Symbol(Type, Decl(index.ts, 0, 8)) + +export const foo = { ...({} as Type) }; +>foo : Symbol(foo, Decl(index.ts, 2, 12)) +>Type : Symbol(Type, Decl(index.ts, 0, 8)) + diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.types b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.types new file mode 100644 index 0000000000000..1e5e4b1c668a0 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum2.types @@ -0,0 +1,34 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameEnum2.ts] //// + +=== type.ts === +export type Type = { x?: { [Enum.A]: 0 } }; +>Type : Type +> : ^^^^ +>x : {} | undefined +> : ^^^^^^^^^^^^^^ +>[Enum.A] : 0 +> : ^ +>Enum.A : any +> : ^^^ +>Enum : any +> : ^^^ +>A : any +> : ^^^ + +=== index.ts === +import { type Type } from "./type"; +>Type : any +> : ^^^ + +export const foo = { ...({} as Type) }; +>foo : { x?: { [Enum.A]: 0; }; } +> : ^^^^^^ ^^^ +>{ ...({} as Type) } : { x?: { [Enum.A]: 0; }; } +> : ^^^^^^ ^^^ +>({} as Type) : Type +> : ^^^^ +>{} as Type : Type +> : ^^^^ +>{} : {} +> : ^^ + diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.errors.txt b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.errors.txt new file mode 100644 index 0000000000000..a960f11f4c2ad --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.errors.txt @@ -0,0 +1,22 @@ +type.ts(7,28): error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. +type.ts(7,28): error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'. + + +==== type.ts (2 errors) ==== + export namespace Foo { + export enum Enum { + A = "a", + B = "b", + } + } + export type Type = { x?: { [Foo.Enum]: 0 } }; + ~~~~~~~~~~ +!!! error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. + ~~~~~~~~~~ +!!! error TS2464: A computed property name must be of type 'string', 'number', 'symbol', or 'any'. + +==== index.ts (0 errors) ==== + import { type Type } from "./type"; + + export const foo = { ...({} as Type) }; + \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.js b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.js new file mode 100644 index 0000000000000..1e3f52a3eafeb --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.js @@ -0,0 +1,33 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameEnum3.ts] //// + +//// [type.ts] +export namespace Foo { + export enum Enum { + A = "a", + B = "b", + } +} +export type Type = { x?: { [Foo.Enum]: 0 } }; + +//// [index.ts] +import { type Type } from "./type"; + +export const foo = { ...({} as Type) }; + + + + +//// [type.d.ts] +export declare namespace Foo { + enum Enum { + A = "a", + B = "b" + } +} +export type Type = { + x?: {}; +}; +//// [index.d.ts] +export declare const foo: { + x?: {}; +}; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.symbols b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.symbols new file mode 100644 index 0000000000000..a465e4b2c7cb0 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.symbols @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameEnum3.ts] //// + +=== type.ts === +export namespace Foo { +>Foo : Symbol(Foo, Decl(type.ts, 0, 0)) + + export enum Enum { +>Enum : Symbol(Enum, Decl(type.ts, 0, 22)) + + A = "a", +>A : Symbol(Enum.A, Decl(type.ts, 1, 20)) + + B = "b", +>B : Symbol(Enum.B, Decl(type.ts, 2, 12)) + } +} +export type Type = { x?: { [Foo.Enum]: 0 } }; +>Type : Symbol(Type, Decl(type.ts, 5, 1)) +>x : Symbol(x, Decl(type.ts, 6, 20)) +>[Foo.Enum] : Symbol([Foo.Enum], Decl(type.ts, 6, 26)) +>Foo.Enum : Symbol(Foo.Enum, Decl(type.ts, 0, 22)) +>Foo : Symbol(Foo, Decl(type.ts, 0, 0)) +>Enum : Symbol(Foo.Enum, Decl(type.ts, 0, 22)) + +=== index.ts === +import { type Type } from "./type"; +>Type : Symbol(Type, Decl(index.ts, 0, 8)) + +export const foo = { ...({} as Type) }; +>foo : Symbol(foo, Decl(index.ts, 2, 12)) +>Type : Symbol(Type, Decl(index.ts, 0, 8)) + diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.types b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.types new file mode 100644 index 0000000000000..9dcab9277a3e9 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameEnum3.types @@ -0,0 +1,55 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameEnum3.ts] //// + +=== type.ts === +export namespace Foo { +>Foo : typeof Foo +> : ^^^^^^^^^^ + + export enum Enum { +>Enum : Enum +> : ^^^^ + + A = "a", +>A : Enum.A +> : ^^^^^^ +>"a" : "a" +> : ^^^ + + B = "b", +>B : Enum.B +> : ^^^^^^ +>"b" : "b" +> : ^^^ + } +} +export type Type = { x?: { [Foo.Enum]: 0 } }; +>Type : Type +> : ^^^^ +>x : {} | undefined +> : ^^^^^^^^^^^^^^ +>[Foo.Enum] : 0 +> : ^ +>Foo.Enum : typeof Foo.Enum +> : ^^^^^^^^^^^^^^^ +>Foo : typeof Foo +> : ^^^^^^^^^^ +>Enum : typeof Foo.Enum +> : ^^^^^^^^^^^^^^^ + +=== index.ts === +import { type Type } from "./type"; +>Type : any +> : ^^^ + +export const foo = { ...({} as Type) }; +>foo : { x?: {}; } +> : ^^^^^^ ^^^ +>{ ...({} as Type) } : { x?: {}; } +> : ^^^^^^ ^^^ +>({} as Type) : Type +> : ^^^^ +>{} as Type : Type +> : ^^^^ +>{} : {} +> : ^^ + diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt new file mode 100644 index 0000000000000..8f3cd5500908b --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt @@ -0,0 +1,16 @@ +type.ts(2,16): error TS1155: 'const' declarations must be initialized. + + +==== type.ts (1 errors) ==== + export namespace Foo { + export const sym: unique symbol; + ~~~ +!!! error TS1155: 'const' declarations must be initialized. + } + export type Type = { x?: { [Foo.sym]: 0 } }; + +==== index.ts (0 errors) ==== + import { type Type } from "./type"; + + export const foo = { ...({} as Type) }; + \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js new file mode 100644 index 0000000000000..ef575b094764c --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js @@ -0,0 +1,31 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts] //// + +//// [type.ts] +export namespace Foo { + export const sym: unique symbol; +} +export type Type = { x?: { [Foo.sym]: 0 } }; + +//// [index.ts] +import { type Type } from "./type"; + +export const foo = { ...({} as Type) }; + + + + +//// [type.d.ts] +export declare namespace Foo { + const sym: unique symbol; +} +export type Type = { + x?: { + [Foo.sym]: 0; + }; +}; +//// [index.d.ts] +export declare const foo: { + x?: { + [Foo.sym]: 0; + }; +}; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols new file mode 100644 index 0000000000000..5634a5d4415ed --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols @@ -0,0 +1,25 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts] //// + +=== type.ts === +export namespace Foo { +>Foo : Symbol(Foo, Decl(type.ts, 0, 0)) + + export const sym: unique symbol; +>sym : Symbol(sym, Decl(type.ts, 1, 14)) +} +export type Type = { x?: { [Foo.sym]: 0 } }; +>Type : Symbol(Type, Decl(type.ts, 2, 1)) +>x : Symbol(x, Decl(type.ts, 3, 20)) +>[Foo.sym] : Symbol([Foo.sym], Decl(type.ts, 3, 26)) +>Foo.sym : Symbol(Foo.sym, Decl(type.ts, 1, 14)) +>Foo : Symbol(Foo, Decl(type.ts, 0, 0)) +>sym : Symbol(Foo.sym, Decl(type.ts, 1, 14)) + +=== index.ts === +import { type Type } from "./type"; +>Type : Symbol(Type, Decl(index.ts, 0, 8)) + +export const foo = { ...({} as Type) }; +>foo : Symbol(foo, Decl(index.ts, 2, 12)) +>Type : Symbol(Type, Decl(index.ts, 0, 8)) + diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types new file mode 100644 index 0000000000000..5c3505118bff7 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types @@ -0,0 +1,42 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts] //// + +=== type.ts === +export namespace Foo { +>Foo : typeof Foo +> : ^^^^^^^^^^ + + export const sym: unique symbol; +>sym : unique symbol +> : ^^^^^^^^^^^^^ +} +export type Type = { x?: { [Foo.sym]: 0 } }; +>Type : Type +> : ^^^^ +>x : { [Foo.sym]: 0; } | undefined +> : ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ +>[Foo.sym] : 0 +> : ^ +>Foo.sym : unique symbol +> : ^^^^^^^^^^^^^ +>Foo : typeof Foo +> : ^^^^^^^^^^ +>sym : unique symbol +> : ^^^^^^^^^^^^^ + +=== index.ts === +import { type Type } from "./type"; +>Type : any +> : ^^^ + +export const foo = { ...({} as Type) }; +>foo : { x?: { [Foo.sym]: 0; }; } +> : ^^^^^^ ^^^ +>{ ...({} as Type) } : { x?: { [Foo.sym]: 0; }; } +> : ^^^^^^ ^^^ +>({} as Type) : Type +> : ^^^^ +>{} as Type : Type +> : ^^^^ +>{} : {} +> : ^^ + diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt new file mode 100644 index 0000000000000..56007da3d7f39 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt @@ -0,0 +1,19 @@ +index.ts(3,14): error TS4023: Exported variable 'foo' has or is using name 'Foo' from external module "type" but cannot be named. +type.ts(2,16): error TS1155: 'const' declarations must be initialized. + + +==== type.ts (1 errors) ==== + namespace Foo { + export const sym: unique symbol; + ~~~ +!!! error TS1155: 'const' declarations must be initialized. + } + export type Type = { x?: { [Foo.sym]: 0 } }; + +==== index.ts (1 errors) ==== + import { type Type } from "./type"; + + export const foo = { ...({} as Type) }; + ~~~ +!!! error TS4023: Exported variable 'foo' has or is using name 'Foo' from external module "type" but cannot be named. + \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js new file mode 100644 index 0000000000000..a8aee96300eff --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js @@ -0,0 +1,26 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts] //// + +//// [type.ts] +namespace Foo { + export const sym: unique symbol; +} +export type Type = { x?: { [Foo.sym]: 0 } }; + +//// [index.ts] +import { type Type } from "./type"; + +export const foo = { ...({} as Type) }; + + + + +//// [type.d.ts] +declare namespace Foo { + const sym: unique symbol; +} +export type Type = { + x?: { + [Foo.sym]: 0; + }; +}; +export {}; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols new file mode 100644 index 0000000000000..90d8b18af3608 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols @@ -0,0 +1,25 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts] //// + +=== type.ts === +namespace Foo { +>Foo : Symbol(Foo, Decl(type.ts, 0, 0)) + + export const sym: unique symbol; +>sym : Symbol(sym, Decl(type.ts, 1, 14)) +} +export type Type = { x?: { [Foo.sym]: 0 } }; +>Type : Symbol(Type, Decl(type.ts, 2, 1)) +>x : Symbol(x, Decl(type.ts, 3, 20)) +>[Foo.sym] : Symbol([Foo.sym], Decl(type.ts, 3, 26)) +>Foo.sym : Symbol(Foo.sym, Decl(type.ts, 1, 14)) +>Foo : Symbol(Foo, Decl(type.ts, 0, 0)) +>sym : Symbol(Foo.sym, Decl(type.ts, 1, 14)) + +=== index.ts === +import { type Type } from "./type"; +>Type : Symbol(Type, Decl(index.ts, 0, 8)) + +export const foo = { ...({} as Type) }; +>foo : Symbol(foo, Decl(index.ts, 2, 12)) +>Type : Symbol(Type, Decl(index.ts, 0, 8)) + diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types new file mode 100644 index 0000000000000..c3b146e8ffd9a --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types @@ -0,0 +1,42 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts] //// + +=== type.ts === +namespace Foo { +>Foo : typeof Foo +> : ^^^^^^^^^^ + + export const sym: unique symbol; +>sym : unique symbol +> : ^^^^^^^^^^^^^ +} +export type Type = { x?: { [Foo.sym]: 0 } }; +>Type : Type +> : ^^^^ +>x : { [Foo.sym]: 0; } | undefined +> : ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ +>[Foo.sym] : 0 +> : ^ +>Foo.sym : unique symbol +> : ^^^^^^^^^^^^^ +>Foo : typeof Foo +> : ^^^^^^^^^^ +>sym : unique symbol +> : ^^^^^^^^^^^^^ + +=== index.ts === +import { type Type } from "./type"; +>Type : any +> : ^^^ + +export const foo = { ...({} as Type) }; +>foo : { x?: { [Foo.sym]: 0; }; } +> : ^^^^^^ ^^^ +>{ ...({} as Type) } : { x?: { [Foo.sym]: 0; }; } +> : ^^^^^^ ^^^ +>({} as Type) : Type +> : ^^^^ +>{} as Type : Type +> : ^^^^ +>{} : {} +> : ^^ + diff --git a/tests/cases/compiler/declarationEmitComputedPropertyNameEnum2.ts b/tests/cases/compiler/declarationEmitComputedPropertyNameEnum2.ts new file mode 100644 index 0000000000000..bfefcba927b7a --- /dev/null +++ b/tests/cases/compiler/declarationEmitComputedPropertyNameEnum2.ts @@ -0,0 +1,11 @@ +// @strict: true +// @declaration: true +// @emitDeclarationOnly: true + +// @filename: type.ts +export type Type = { x?: { [Enum.A]: 0 } }; + +// @filename: index.ts +import { type Type } from "./type"; + +export const foo = { ...({} as Type) }; diff --git a/tests/cases/compiler/declarationEmitComputedPropertyNameEnum3.ts b/tests/cases/compiler/declarationEmitComputedPropertyNameEnum3.ts new file mode 100644 index 0000000000000..ecb796c0a0733 --- /dev/null +++ b/tests/cases/compiler/declarationEmitComputedPropertyNameEnum3.ts @@ -0,0 +1,17 @@ +// @strict: true +// @declaration: true +// @emitDeclarationOnly: true + +// @filename: type.ts +export namespace Foo { + export enum Enum { + A = "a", + B = "b", + } +} +export type Type = { x?: { [Foo.Enum]: 0 } }; + +// @filename: index.ts +import { type Type } from "./type"; + +export const foo = { ...({} as Type) }; diff --git a/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts new file mode 100644 index 0000000000000..12279205dd6c9 --- /dev/null +++ b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts @@ -0,0 +1,14 @@ +// @strict: true +// @declaration: true +// @emitDeclarationOnly: true + +// @filename: type.ts +export namespace Foo { + export const sym: unique symbol; +} +export type Type = { x?: { [Foo.sym]: 0 } }; + +// @filename: index.ts +import { type Type } from "./type"; + +export const foo = { ...({} as Type) }; diff --git a/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts new file mode 100644 index 0000000000000..0eb06b89ac7ed --- /dev/null +++ b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts @@ -0,0 +1,14 @@ +// @strict: true +// @declaration: true +// @emitDeclarationOnly: true + +// @filename: type.ts +namespace Foo { + export const sym: unique symbol; +} +export type Type = { x?: { [Foo.sym]: 0 } }; + +// @filename: index.ts +import { type Type } from "./type"; + +export const foo = { ...({} as Type) }; From 2a0a4a07b257d876d5280a1fca51c35ef7d34dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Thu, 6 Jun 2024 23:33:31 +0200 Subject: [PATCH 4/6] fixed symbol test cases --- ...EmitComputedPropertyNameSymbol1.errors.txt | 13 ++++++----- ...larationEmitComputedPropertyNameSymbol1.js | 8 +++---- ...ionEmitComputedPropertyNameSymbol1.symbols | 2 +- ...ationEmitComputedPropertyNameSymbol1.types | 22 +++++++++++-------- ...EmitComputedPropertyNameSymbol2.errors.txt | 18 +++++++-------- ...larationEmitComputedPropertyNameSymbol2.js | 10 ++++----- ...ionEmitComputedPropertyNameSymbol2.symbols | 2 +- ...ationEmitComputedPropertyNameSymbol2.types | 22 +++++++++++-------- ...larationEmitComputedPropertyNameSymbol1.ts | 2 +- ...larationEmitComputedPropertyNameSymbol2.ts | 2 +- 10 files changed, 55 insertions(+), 46 deletions(-) diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt index 8f3cd5500908b..6658302ef22e6 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt @@ -1,13 +1,16 @@ -type.ts(2,16): error TS1155: 'const' declarations must be initialized. +type.ts(2,22): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later. +type.ts(4,28): error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. -==== type.ts (1 errors) ==== +==== type.ts (2 errors) ==== export namespace Foo { - export const sym: unique symbol; - ~~~ -!!! error TS1155: 'const' declarations must be initialized. + export const sym = Symbol(); + ~~~~~~ +!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later. } export type Type = { x?: { [Foo.sym]: 0 } }; + ~~~~~~~~~ +!!! error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. ==== index.ts (0 errors) ==== import { type Type } from "./type"; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js index ef575b094764c..ef781c112641f 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js @@ -2,7 +2,7 @@ //// [type.ts] export namespace Foo { - export const sym: unique symbol; + export const sym = Symbol(); } export type Type = { x?: { [Foo.sym]: 0 } }; @@ -16,12 +16,10 @@ export const foo = { ...({} as Type) }; //// [type.d.ts] export declare namespace Foo { - const sym: unique symbol; + const sym: any; } export type Type = { - x?: { - [Foo.sym]: 0; - }; + x?: {}; }; //// [index.d.ts] export declare const foo: { diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols index 5634a5d4415ed..f8fa8edd605cf 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols @@ -4,7 +4,7 @@ export namespace Foo { >Foo : Symbol(Foo, Decl(type.ts, 0, 0)) - export const sym: unique symbol; + export const sym = Symbol(); >sym : Symbol(sym, Decl(type.ts, 1, 14)) } export type Type = { x?: { [Foo.sym]: 0 } }; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types index 5c3505118bff7..9411b8d6a8915 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types @@ -5,23 +5,27 @@ export namespace Foo { >Foo : typeof Foo > : ^^^^^^^^^^ - export const sym: unique symbol; ->sym : unique symbol -> : ^^^^^^^^^^^^^ + export const sym = Symbol(); +>sym : any +> : ^^^ +>Symbol() : any +> : ^^^ +>Symbol : any +> : ^^^ } export type Type = { x?: { [Foo.sym]: 0 } }; >Type : Type > : ^^^^ ->x : { [Foo.sym]: 0; } | undefined -> : ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ +>x : {} | undefined +> : ^^^^^^^^^^^^^^ >[Foo.sym] : 0 > : ^ ->Foo.sym : unique symbol -> : ^^^^^^^^^^^^^ +>Foo.sym : any +> : ^^^ >Foo : typeof Foo > : ^^^^^^^^^^ ->sym : unique symbol -> : ^^^^^^^^^^^^^ +>sym : any +> : ^^^ === index.ts === import { type Type } from "./type"; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt index 56007da3d7f39..cb483be97e72d 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt @@ -1,19 +1,19 @@ -index.ts(3,14): error TS4023: Exported variable 'foo' has or is using name 'Foo' from external module "type" but cannot be named. -type.ts(2,16): error TS1155: 'const' declarations must be initialized. +type.ts(2,22): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later. +type.ts(4,28): error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. -==== type.ts (1 errors) ==== +==== type.ts (2 errors) ==== namespace Foo { - export const sym: unique symbol; - ~~~ -!!! error TS1155: 'const' declarations must be initialized. + export const sym = Symbol(); + ~~~~~~ +!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later. } export type Type = { x?: { [Foo.sym]: 0 } }; + ~~~~~~~~~ +!!! error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. -==== index.ts (1 errors) ==== +==== index.ts (0 errors) ==== import { type Type } from "./type"; export const foo = { ...({} as Type) }; - ~~~ -!!! error TS4023: Exported variable 'foo' has or is using name 'Foo' from external module "type" but cannot be named. \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js index a8aee96300eff..c379b0fc0beb9 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js @@ -2,7 +2,7 @@ //// [type.ts] namespace Foo { - export const sym: unique symbol; + export const sym = Symbol(); } export type Type = { x?: { [Foo.sym]: 0 } }; @@ -15,12 +15,12 @@ export const foo = { ...({} as Type) }; //// [type.d.ts] -declare namespace Foo { - const sym: unique symbol; -} export type Type = { + x?: {}; +}; +//// [index.d.ts] +export declare const foo: { x?: { [Foo.sym]: 0; }; }; -export {}; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols index 90d8b18af3608..0f73bd4526497 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols @@ -4,7 +4,7 @@ namespace Foo { >Foo : Symbol(Foo, Decl(type.ts, 0, 0)) - export const sym: unique symbol; + export const sym = Symbol(); >sym : Symbol(sym, Decl(type.ts, 1, 14)) } export type Type = { x?: { [Foo.sym]: 0 } }; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types index c3b146e8ffd9a..2a793a2bd8283 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types @@ -5,23 +5,27 @@ namespace Foo { >Foo : typeof Foo > : ^^^^^^^^^^ - export const sym: unique symbol; ->sym : unique symbol -> : ^^^^^^^^^^^^^ + export const sym = Symbol(); +>sym : any +> : ^^^ +>Symbol() : any +> : ^^^ +>Symbol : any +> : ^^^ } export type Type = { x?: { [Foo.sym]: 0 } }; >Type : Type > : ^^^^ ->x : { [Foo.sym]: 0; } | undefined -> : ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ +>x : {} | undefined +> : ^^^^^^^^^^^^^^ >[Foo.sym] : 0 > : ^ ->Foo.sym : unique symbol -> : ^^^^^^^^^^^^^ +>Foo.sym : any +> : ^^^ >Foo : typeof Foo > : ^^^^^^^^^^ ->sym : unique symbol -> : ^^^^^^^^^^^^^ +>sym : any +> : ^^^ === index.ts === import { type Type } from "./type"; diff --git a/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts index 12279205dd6c9..71937a0088337 100644 --- a/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts +++ b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts @@ -4,7 +4,7 @@ // @filename: type.ts export namespace Foo { - export const sym: unique symbol; + export const sym = Symbol(); } export type Type = { x?: { [Foo.sym]: 0 } }; diff --git a/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts index 0eb06b89ac7ed..0632e14110650 100644 --- a/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts +++ b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts @@ -4,7 +4,7 @@ // @filename: type.ts namespace Foo { - export const sym: unique symbol; + export const sym = Symbol(); } export type Type = { x?: { [Foo.sym]: 0 } }; From 52d1f44d2c6991da2faf3b0b93f4f8608bd04e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Thu, 6 Jun 2024 23:34:54 +0200 Subject: [PATCH 5/6] use correct lib --- ...EmitComputedPropertyNameSymbol1.errors.txt | 19 ---------- ...larationEmitComputedPropertyNameSymbol1.js | 36 +++++++++++++++++-- ...ionEmitComputedPropertyNameSymbol1.symbols | 1 + ...ationEmitComputedPropertyNameSymbol1.types | 24 ++++++------- ...EmitComputedPropertyNameSymbol2.errors.txt | 13 +++---- ...larationEmitComputedPropertyNameSymbol2.js | 8 ++--- ...ionEmitComputedPropertyNameSymbol2.symbols | 1 + ...ationEmitComputedPropertyNameSymbol2.types | 24 ++++++------- ...larationEmitComputedPropertyNameSymbol1.ts | 1 + ...larationEmitComputedPropertyNameSymbol2.ts | 1 + 10 files changed, 71 insertions(+), 57 deletions(-) delete mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt deleted file mode 100644 index 6658302ef22e6..0000000000000 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt +++ /dev/null @@ -1,19 +0,0 @@ -type.ts(2,22): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later. -type.ts(4,28): error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. - - -==== type.ts (2 errors) ==== - export namespace Foo { - export const sym = Symbol(); - ~~~~~~ -!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later. - } - export type Type = { x?: { [Foo.sym]: 0 } }; - ~~~~~~~~~ -!!! error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. - -==== index.ts (0 errors) ==== - import { type Type } from "./type"; - - export const foo = { ...({} as Type) }; - \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js index ef781c112641f..71ae578a1fb9e 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js @@ -16,10 +16,12 @@ export const foo = { ...({} as Type) }; //// [type.d.ts] export declare namespace Foo { - const sym: any; + const sym: unique symbol; } export type Type = { - x?: {}; + x?: { + [Foo.sym]: 0; + }; }; //// [index.d.ts] export declare const foo: { @@ -27,3 +29,33 @@ export declare const foo: { [Foo.sym]: 0; }; }; + + +//// [DtsFileErrors] + + +index.d.ts(3,9): error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. +index.d.ts(3,10): error TS2552: Cannot find name 'Foo'. Did you mean 'foo'? + + +==== type.d.ts (0 errors) ==== + export declare namespace Foo { + const sym: unique symbol; + } + export type Type = { + x?: { + [Foo.sym]: 0; + }; + }; + +==== index.d.ts (2 errors) ==== + export declare const foo: { + x?: { + [Foo.sym]: 0; + ~~~~~~~~~ +!!! error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. + ~~~ +!!! error TS2552: Cannot find name 'Foo'. Did you mean 'foo'? + }; + }; + \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols index f8fa8edd605cf..ba2dd2469bc6d 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols @@ -6,6 +6,7 @@ export namespace Foo { export const sym = Symbol(); >sym : Symbol(sym, Decl(type.ts, 1, 14)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) } export type Type = { x?: { [Foo.sym]: 0 } }; >Type : Symbol(Type, Decl(type.ts, 2, 1)) diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types index 9411b8d6a8915..2e8c9289d3ea2 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types @@ -6,26 +6,26 @@ export namespace Foo { > : ^^^^^^^^^^ export const sym = Symbol(); ->sym : any -> : ^^^ ->Symbol() : any -> : ^^^ ->Symbol : any -> : ^^^ +>sym : unique symbol +> : ^^^^^^^^^^^^^ +>Symbol() : unique symbol +> : ^^^^^^^^^^^^^ +>Symbol : SymbolConstructor +> : ^^^^^^^^^^^^^^^^^ } export type Type = { x?: { [Foo.sym]: 0 } }; >Type : Type > : ^^^^ ->x : {} | undefined -> : ^^^^^^^^^^^^^^ +>x : { [Foo.sym]: 0; } | undefined +> : ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ >[Foo.sym] : 0 > : ^ ->Foo.sym : any -> : ^^^ +>Foo.sym : unique symbol +> : ^^^^^^^^^^^^^ >Foo : typeof Foo > : ^^^^^^^^^^ ->sym : any -> : ^^^ +>sym : unique symbol +> : ^^^^^^^^^^^^^ === index.ts === import { type Type } from "./type"; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt index cb483be97e72d..16404065d363c 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.errors.txt @@ -1,19 +1,16 @@ -type.ts(2,22): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later. -type.ts(4,28): error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. +index.ts(3,14): error TS4023: Exported variable 'foo' has or is using name 'Foo' from external module "type" but cannot be named. -==== type.ts (2 errors) ==== +==== type.ts (0 errors) ==== namespace Foo { export const sym = Symbol(); - ~~~~~~ -!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later. } export type Type = { x?: { [Foo.sym]: 0 } }; - ~~~~~~~~~ -!!! error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. -==== index.ts (0 errors) ==== +==== index.ts (1 errors) ==== import { type Type } from "./type"; export const foo = { ...({} as Type) }; + ~~~ +!!! error TS4023: Exported variable 'foo' has or is using name 'Foo' from external module "type" but cannot be named. \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js index c379b0fc0beb9..dfd3924681d59 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js @@ -15,12 +15,12 @@ export const foo = { ...({} as Type) }; //// [type.d.ts] +declare namespace Foo { + const sym: unique symbol; +} export type Type = { - x?: {}; -}; -//// [index.d.ts] -export declare const foo: { x?: { [Foo.sym]: 0; }; }; +export {}; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols index 0f73bd4526497..57413c06af8ba 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols @@ -6,6 +6,7 @@ namespace Foo { export const sym = Symbol(); >sym : Symbol(sym, Decl(type.ts, 1, 14)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) } export type Type = { x?: { [Foo.sym]: 0 } }; >Type : Symbol(Type, Decl(type.ts, 2, 1)) diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types index 2a793a2bd8283..0b2bf47965560 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types @@ -6,26 +6,26 @@ namespace Foo { > : ^^^^^^^^^^ export const sym = Symbol(); ->sym : any -> : ^^^ ->Symbol() : any -> : ^^^ ->Symbol : any -> : ^^^ +>sym : unique symbol +> : ^^^^^^^^^^^^^ +>Symbol() : unique symbol +> : ^^^^^^^^^^^^^ +>Symbol : SymbolConstructor +> : ^^^^^^^^^^^^^^^^^ } export type Type = { x?: { [Foo.sym]: 0 } }; >Type : Type > : ^^^^ ->x : {} | undefined -> : ^^^^^^^^^^^^^^ +>x : { [Foo.sym]: 0; } | undefined +> : ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ >[Foo.sym] : 0 > : ^ ->Foo.sym : any -> : ^^^ +>Foo.sym : unique symbol +> : ^^^^^^^^^^^^^ >Foo : typeof Foo > : ^^^^^^^^^^ ->sym : any -> : ^^^ +>sym : unique symbol +> : ^^^^^^^^^^^^^ === index.ts === import { type Type } from "./type"; diff --git a/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts index 71937a0088337..34d3db09fed9f 100644 --- a/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts +++ b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts @@ -1,4 +1,5 @@ // @strict: true +// @lib: esnext // @declaration: true // @emitDeclarationOnly: true diff --git a/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts index 0632e14110650..42e2b86ce8939 100644 --- a/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts +++ b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts @@ -1,4 +1,5 @@ // @strict: true +// @lib: esnext // @declaration: true // @emitDeclarationOnly: true From 227a0f6a3527239990dd29c0db0ddd6f9ba9f0e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Fri, 7 Jun 2024 00:07:43 +0200 Subject: [PATCH 6/6] fixed reporter error --- src/compiler/checker.ts | 3 ++ ...EmitComputedPropertyNameSymbol1.errors.txt | 16 +++++++++ ...larationEmitComputedPropertyNameSymbol1.js | 36 ------------------- 3 files changed, 19 insertions(+), 36 deletions(-) create mode 100644 tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 19207b4295bcd..927e63a3d5320 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8866,6 +8866,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { typeof evaluated.value === "number" ? factory.createNumericLiteral(evaluated.value, /*numericLiteralFlags*/ 0) : undefined; if (!literalNode) { + if (isImportTypeNode(computedPropertyNameType)) { + trackComputedName(node.expression, context.enclosingDeclaration, context); + } return node; } literal = literalNode; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt new file mode 100644 index 0000000000000..053c332709b33 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.errors.txt @@ -0,0 +1,16 @@ +index.ts(3,14): error TS4023: Exported variable 'foo' has or is using name 'Foo' from external module "type" but cannot be named. + + +==== type.ts (0 errors) ==== + export namespace Foo { + export const sym = Symbol(); + } + export type Type = { x?: { [Foo.sym]: 0 } }; + +==== index.ts (1 errors) ==== + import { type Type } from "./type"; + + export const foo = { ...({} as Type) }; + ~~~ +!!! error TS4023: Exported variable 'foo' has or is using name 'Foo' from external module "type" but cannot be named. + \ No newline at end of file diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js index 71ae578a1fb9e..1f4a701d59a9a 100644 --- a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js @@ -23,39 +23,3 @@ export type Type = { [Foo.sym]: 0; }; }; -//// [index.d.ts] -export declare const foo: { - x?: { - [Foo.sym]: 0; - }; -}; - - -//// [DtsFileErrors] - - -index.d.ts(3,9): error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. -index.d.ts(3,10): error TS2552: Cannot find name 'Foo'. Did you mean 'foo'? - - -==== type.d.ts (0 errors) ==== - export declare namespace Foo { - const sym: unique symbol; - } - export type Type = { - x?: { - [Foo.sym]: 0; - }; - }; - -==== index.d.ts (2 errors) ==== - export declare const foo: { - x?: { - [Foo.sym]: 0; - ~~~~~~~~~ -!!! error TS1170: A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. - ~~~ -!!! error TS2552: Cannot find name 'Foo'. Did you mean 'foo'? - }; - }; - \ No newline at end of file