From 552b07e795ec5db98c37fd4ace730133bbf0e781 Mon Sep 17 00:00:00 2001 From: TypeScript Bot Date: Fri, 14 Jun 2024 18:21:52 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Pick=20PR=20#58786=20(Fixed=20de?= =?UTF-8?q?claration=20emit=20crash=20relate...)=20into=20release-5.5=20(#?= =?UTF-8?q?58853)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mateusz BurzyƄski --- src/compiler/checker.ts | 19 ++++++- ...eclarationEmitComputedPropertyNameEnum1.js | 34 ++++++++++++ ...ationEmitComputedPropertyNameEnum1.symbols | 29 ++++++++++ ...arationEmitComputedPropertyNameEnum1.types | 51 +++++++++++++++++ ...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 | 25 +++++++++ ...ionEmitComputedPropertyNameSymbol1.symbols | 26 +++++++++ ...ationEmitComputedPropertyNameSymbol1.types | 46 ++++++++++++++++ ...EmitComputedPropertyNameSymbol2.errors.txt | 16 ++++++ ...larationEmitComputedPropertyNameSymbol2.js | 26 +++++++++ ...ionEmitComputedPropertyNameSymbol2.symbols | 26 +++++++++ ...ationEmitComputedPropertyNameSymbol2.types | 46 ++++++++++++++++ ...eclarationEmitComputedPropertyNameEnum1.ts | 16 ++++++ ...eclarationEmitComputedPropertyNameEnum2.ts | 11 ++++ ...eclarationEmitComputedPropertyNameEnum3.ts | 17 ++++++ ...larationEmitComputedPropertyNameSymbol1.ts | 15 +++++ ...larationEmitComputedPropertyNameSymbol2.ts | 15 +++++ 25 files changed, 663 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/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/declarationEmitComputedPropertyNameEnum1.ts 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 5a0e11d8d8d70..68c522712daf6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8840,8 +8840,23 @@ 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; + if (!literalNode) { + if (isImportTypeNode(computedPropertyNameType)) { + trackComputedName(node.expression, context.enclosingDeclaration, context); + } + return node; + } + 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/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..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 new file mode 100644 index 0000000000000..1f4a701d59a9a --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.js @@ -0,0 +1,25 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts] //// + +//// [type.ts] +export namespace Foo { + export const sym = 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; + }; +}; diff --git a/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols new file mode 100644 index 0000000000000..ba2dd2469bc6d --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.symbols @@ -0,0 +1,26 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts] //// + +=== type.ts === +export namespace Foo { +>Foo : Symbol(Foo, Decl(type.ts, 0, 0)) + + 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)) +>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..2e8c9289d3ea2 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol1.types @@ -0,0 +1,46 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts] //// + +=== type.ts === +export namespace Foo { +>Foo : typeof Foo +> : ^^^^^^^^^^ + + export const sym = Symbol(); +>sym : unique symbol +> : ^^^^^^^^^^^^^ +>Symbol() : unique symbol +> : ^^^^^^^^^^^^^ +>Symbol : SymbolConstructor +> : ^^^^^^^^^^^^^^^^^ +} +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..16404065d363c --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.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) ==== + 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/declarationEmitComputedPropertyNameSymbol2.js b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js new file mode 100644 index 0000000000000..dfd3924681d59 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.js @@ -0,0 +1,26 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts] //// + +//// [type.ts] +namespace Foo { + export const sym = 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..57413c06af8ba --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.symbols @@ -0,0 +1,26 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts] //// + +=== type.ts === +namespace Foo { +>Foo : Symbol(Foo, Decl(type.ts, 0, 0)) + + 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)) +>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..0b2bf47965560 --- /dev/null +++ b/tests/baselines/reference/declarationEmitComputedPropertyNameSymbol2.types @@ -0,0 +1,46 @@ +//// [tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts] //// + +=== type.ts === +namespace Foo { +>Foo : typeof Foo +> : ^^^^^^^^^^ + + export const sym = Symbol(); +>sym : unique symbol +> : ^^^^^^^^^^^^^ +>Symbol() : unique symbol +> : ^^^^^^^^^^^^^ +>Symbol : SymbolConstructor +> : ^^^^^^^^^^^^^^^^^ +} +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/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) }; 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..34d3db09fed9f --- /dev/null +++ b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol1.ts @@ -0,0 +1,15 @@ +// @strict: true +// @lib: esnext +// @declaration: true +// @emitDeclarationOnly: true + +// @filename: type.ts +export namespace Foo { + export const sym = 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..42e2b86ce8939 --- /dev/null +++ b/tests/cases/compiler/declarationEmitComputedPropertyNameSymbol2.ts @@ -0,0 +1,15 @@ +// @strict: true +// @lib: esnext +// @declaration: true +// @emitDeclarationOnly: true + +// @filename: type.ts +namespace Foo { + export const sym = Symbol(); +} +export type Type = { x?: { [Foo.sym]: 0 } }; + +// @filename: index.ts +import { type Type } from "./type"; + +export const foo = { ...({} as Type) };