From ddba8c90d0f78665d115bd78b215e3ebb9d4492c Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 2 May 2024 14:18:36 -0700 Subject: [PATCH] Type alias declarations should not return an effective annotation node --- src/compiler/utilities.ts | 1 + .../declarationEmitMergedAliasWithConst.js | 31 ++++++++++++++ ...eclarationEmitMergedAliasWithConst.symbols | 27 ++++++++++++ .../declarationEmitMergedAliasWithConst.types | 41 +++++++++++++++++++ .../declarationEmitMergedAliasWithConst.ts | 9 ++++ 5 files changed, 109 insertions(+) create mode 100644 tests/baselines/reference/declarationEmitMergedAliasWithConst.js create mode 100644 tests/baselines/reference/declarationEmitMergedAliasWithConst.symbols create mode 100644 tests/baselines/reference/declarationEmitMergedAliasWithConst.types create mode 100644 tests/cases/compiler/declarationEmitMergedAliasWithConst.ts diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index b22ba836761e2..1fffa5af2bf7a 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -6668,6 +6668,7 @@ export function getAllAccessorDeclarations(declarations: readonly Declaration[] */ export function getEffectiveTypeAnnotationNode(node: Node): TypeNode | undefined { if (!isInJSFile(node) && isFunctionDeclaration(node)) return undefined; + if (isTypeAliasDeclaration(node)) return undefined; // has a .type, is not a type annotation const type = (node as HasType).type; if (type || !isInJSFile(node)) return type; return isJSDocPropertyLikeTag(node) ? node.typeExpression && node.typeExpression.type : getJSDocType(node); diff --git a/tests/baselines/reference/declarationEmitMergedAliasWithConst.js b/tests/baselines/reference/declarationEmitMergedAliasWithConst.js new file mode 100644 index 0000000000000..5e2f0456feaac --- /dev/null +++ b/tests/baselines/reference/declarationEmitMergedAliasWithConst.js @@ -0,0 +1,31 @@ +//// [tests/cases/compiler/declarationEmitMergedAliasWithConst.ts] //// + +//// [declarationEmitMergedAliasWithConst.ts] +export const Color = { + Red: "Red", + Green: "Green", + Blue: "Blue" +} as const + +export type Color = typeof Color +export type Colors = Color[keyof Color] + +//// [declarationEmitMergedAliasWithConst.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Color = void 0; +exports.Color = { + Red: "Red", + Green: "Green", + Blue: "Blue" +}; + + +//// [declarationEmitMergedAliasWithConst.d.ts] +export declare const Color: { + readonly Red: "Red"; + readonly Green: "Green"; + readonly Blue: "Blue"; +}; +export type Color = typeof Color; +export type Colors = Color[keyof Color]; diff --git a/tests/baselines/reference/declarationEmitMergedAliasWithConst.symbols b/tests/baselines/reference/declarationEmitMergedAliasWithConst.symbols new file mode 100644 index 0000000000000..46b643ca9e1e3 --- /dev/null +++ b/tests/baselines/reference/declarationEmitMergedAliasWithConst.symbols @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/declarationEmitMergedAliasWithConst.ts] //// + +=== declarationEmitMergedAliasWithConst.ts === +export const Color = { +>Color : Symbol(Color, Decl(declarationEmitMergedAliasWithConst.ts, 0, 12), Decl(declarationEmitMergedAliasWithConst.ts, 4, 10)) + + Red: "Red", +>Red : Symbol(Red, Decl(declarationEmitMergedAliasWithConst.ts, 0, 22)) + + Green: "Green", +>Green : Symbol(Green, Decl(declarationEmitMergedAliasWithConst.ts, 1, 15)) + + Blue: "Blue" +>Blue : Symbol(Blue, Decl(declarationEmitMergedAliasWithConst.ts, 2, 19)) + +} as const +>const : Symbol(const) + +export type Color = typeof Color +>Color : Symbol(Color, Decl(declarationEmitMergedAliasWithConst.ts, 0, 12), Decl(declarationEmitMergedAliasWithConst.ts, 4, 10)) +>Color : Symbol(Color, Decl(declarationEmitMergedAliasWithConst.ts, 0, 12), Decl(declarationEmitMergedAliasWithConst.ts, 4, 10)) + +export type Colors = Color[keyof Color] +>Colors : Symbol(Colors, Decl(declarationEmitMergedAliasWithConst.ts, 6, 32)) +>Color : Symbol(Color, Decl(declarationEmitMergedAliasWithConst.ts, 0, 12), Decl(declarationEmitMergedAliasWithConst.ts, 4, 10)) +>Color : Symbol(Color, Decl(declarationEmitMergedAliasWithConst.ts, 0, 12), Decl(declarationEmitMergedAliasWithConst.ts, 4, 10)) + diff --git a/tests/baselines/reference/declarationEmitMergedAliasWithConst.types b/tests/baselines/reference/declarationEmitMergedAliasWithConst.types new file mode 100644 index 0000000000000..ef0d06b1b9562 --- /dev/null +++ b/tests/baselines/reference/declarationEmitMergedAliasWithConst.types @@ -0,0 +1,41 @@ +//// [tests/cases/compiler/declarationEmitMergedAliasWithConst.ts] //// + +=== declarationEmitMergedAliasWithConst.ts === +export const Color = { +>Color : { readonly Red: "Red"; readonly Green: "Green"; readonly Blue: "Blue"; } +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>{ Red: "Red", Green: "Green", Blue: "Blue"} as const : { readonly Red: "Red"; readonly Green: "Green"; readonly Blue: "Blue"; } +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>{ Red: "Red", Green: "Green", Blue: "Blue"} : { readonly Red: "Red"; readonly Green: "Green"; readonly Blue: "Blue"; } +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Red: "Red", +>Red : "Red" +> : ^^^^^ +>"Red" : "Red" +> : ^^^^^ + + Green: "Green", +>Green : "Green" +> : ^^^^^^^ +>"Green" : "Green" +> : ^^^^^^^ + + Blue: "Blue" +>Blue : "Blue" +> : ^^^^^^ +>"Blue" : "Blue" +> : ^^^^^^ + +} as const + +export type Color = typeof Color +>Color : { readonly Red: "Red"; readonly Green: "Green"; readonly Blue: "Blue"; } +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>Color : { readonly Red: "Red"; readonly Green: "Green"; readonly Blue: "Blue"; } +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +export type Colors = Color[keyof Color] +>Colors : Colors +> : ^^^^^^ + diff --git a/tests/cases/compiler/declarationEmitMergedAliasWithConst.ts b/tests/cases/compiler/declarationEmitMergedAliasWithConst.ts new file mode 100644 index 0000000000000..ea3167e4ca815 --- /dev/null +++ b/tests/cases/compiler/declarationEmitMergedAliasWithConst.ts @@ -0,0 +1,9 @@ +// @declaration: true +export const Color = { + Red: "Red", + Green: "Green", + Blue: "Blue" +} as const + +export type Color = typeof Color +export type Colors = Color[keyof Color] \ No newline at end of file