Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type alias declarations should not return an effective annotation node #58410

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions tests/baselines/reference/declarationEmitMergedAliasWithConst.js
Original file line number Diff line number Diff line change
@@ -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];
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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
> : ^^^^^^

9 changes: 9 additions & 0 deletions tests/cases/compiler/declarationEmitMergedAliasWithConst.ts
Original file line number Diff line number Diff line change
@@ -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]