diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6ebbd83e37599..fe8cdcf2633ed 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2121,12 +2121,16 @@ namespace ts { } function buildTypeDisplay(type: Type, writer: SymbolWriter, enclosingDeclaration?: Node, globalFlags?: TypeFormatFlags, symbolStack?: Symbol[]) { - const globalFlagsToPass = globalFlags & TypeFormatFlags.WriteOwnNameForAnyLike; + let globalFlagsToPass = globalFlags & TypeFormatFlags.WriteOwnNameForAnyLike; + if (globalFlags & TypeFormatFlags.UseTypeAliasValue) { + globalFlagsToPass |= TypeFormatFlags.UseTypeAliasValue; + } let inObjectTypeLiteral = false; return writeType(type, globalFlags); function writeType(type: Type, flags: TypeFormatFlags) { const nextFlags = flags & ~TypeFormatFlags.InTypeAlias; + // Write undefined/null type as any if (type.flags & TypeFlags.Intrinsic) { // Special handling for unknown / resolving types, they should show up as any and not unknown or __resolving @@ -2152,14 +2156,9 @@ namespace ts { // The specified symbol flags need to be reinterpreted as type flags buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, nextFlags); } - else if (!(flags & TypeFormatFlags.InTypeAlias) && type.flags & (TypeFlags.Anonymous | TypeFlags.UnionOrIntersection) && type.aliasSymbol) { - if (type.flags & TypeFlags.Anonymous || !(flags & TypeFormatFlags.UseTypeAliasValue)) { - const typeArguments = type.aliasTypeArguments; - writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); - } - else { - writeUnionOrIntersectionType(type, nextFlags); - } + else if (!(flags & TypeFormatFlags.InTypeAlias) && type.flags & (TypeFlags.Anonymous | TypeFlags.UnionOrIntersection) && type.aliasSymbol && !(flags & TypeFormatFlags.UseTypeAliasValue)) { + const typeArguments = type.aliasTypeArguments; + writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); } else if (type.flags & TypeFlags.UnionOrIntersection) { writeUnionOrIntersectionType(type, nextFlags); diff --git a/tests/baselines/reference/declFileTypeAliasWithGeneric.js b/tests/baselines/reference/declFileTypeAliasWithGeneric.js new file mode 100644 index 0000000000000..f0d959219c14a --- /dev/null +++ b/tests/baselines/reference/declFileTypeAliasWithGeneric.js @@ -0,0 +1,14 @@ +//// [declFileTypeAliasWithGeneric.ts] +export type Bar = () => [X, Y]; +export type Foo = Bar; +export const y = (x: Foo) => 1 + +//// [declFileTypeAliasWithGeneric.js] +"use strict"; +exports.y = function (x) { return 1; }; + + +//// [declFileTypeAliasWithGeneric.d.ts] +export declare type Bar = () => [X, Y]; +export declare type Foo = Bar; +export declare const y: (x: () => [any, string]) => number; diff --git a/tests/baselines/reference/declFileTypeAliasWithGeneric.symbols b/tests/baselines/reference/declFileTypeAliasWithGeneric.symbols new file mode 100644 index 0000000000000..4c53c9857ed42 --- /dev/null +++ b/tests/baselines/reference/declFileTypeAliasWithGeneric.symbols @@ -0,0 +1,19 @@ +=== tests/cases/compiler/declFileTypeAliasWithGeneric.ts === +export type Bar = () => [X, Y]; +>Bar : Symbol(Bar, Decl(declFileTypeAliasWithGeneric.ts, 0, 0)) +>X : Symbol(X, Decl(declFileTypeAliasWithGeneric.ts, 0, 16)) +>Y : Symbol(Y, Decl(declFileTypeAliasWithGeneric.ts, 0, 18)) +>X : Symbol(X, Decl(declFileTypeAliasWithGeneric.ts, 0, 16)) +>Y : Symbol(Y, Decl(declFileTypeAliasWithGeneric.ts, 0, 18)) + +export type Foo = Bar; +>Foo : Symbol(Foo, Decl(declFileTypeAliasWithGeneric.ts, 0, 37)) +>Y : Symbol(Y, Decl(declFileTypeAliasWithGeneric.ts, 1, 16)) +>Bar : Symbol(Bar, Decl(declFileTypeAliasWithGeneric.ts, 0, 0)) +>Y : Symbol(Y, Decl(declFileTypeAliasWithGeneric.ts, 1, 16)) + +export const y = (x: Foo) => 1 +>y : Symbol(y, Decl(declFileTypeAliasWithGeneric.ts, 2, 12)) +>x : Symbol(x, Decl(declFileTypeAliasWithGeneric.ts, 2, 18)) +>Foo : Symbol(Foo, Decl(declFileTypeAliasWithGeneric.ts, 0, 37)) + diff --git a/tests/baselines/reference/declFileTypeAliasWithGeneric.types b/tests/baselines/reference/declFileTypeAliasWithGeneric.types new file mode 100644 index 0000000000000..a11e0c41e18d8 --- /dev/null +++ b/tests/baselines/reference/declFileTypeAliasWithGeneric.types @@ -0,0 +1,21 @@ +=== tests/cases/compiler/declFileTypeAliasWithGeneric.ts === +export type Bar = () => [X, Y]; +>Bar : Bar +>X : X +>Y : Y +>X : X +>Y : Y + +export type Foo = Bar; +>Foo : Bar +>Y : Y +>Bar : Bar +>Y : Y + +export const y = (x: Foo) => 1 +>y : (x: Bar) => number +>(x: Foo) => 1 : (x: Bar) => number +>x : Bar +>Foo : Bar +>1 : number + diff --git a/tests/cases/compiler/declFileTypeAliasWithGeneric.ts b/tests/cases/compiler/declFileTypeAliasWithGeneric.ts new file mode 100644 index 0000000000000..f2bae04b89809 --- /dev/null +++ b/tests/cases/compiler/declFileTypeAliasWithGeneric.ts @@ -0,0 +1,4 @@ +// @declaration: true +export type Bar = () => [X, Y]; +export type Foo = Bar; +export const y = (x: Foo) => 1 \ No newline at end of file