From 7945eb6f1a51b993a4835ddf66e4a0485957406d Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 28 Sep 2016 14:51:33 -0700 Subject: [PATCH 1/2] Only emit inferred type-alias if it is fully instantiated --- src/compiler/checker.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 61cf701eb95..528ccce2661 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2182,9 +2182,14 @@ 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 && + else if (!(flags & TypeFormatFlags.InTypeAlias) && ((type.flags & TypeFlags.Anonymous && !(type).target) || type.flags & TypeFlags.UnionOrIntersection) && type.aliasSymbol && isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible) { - // Only write out inferred type with its corresponding type-alias if type-alias is visible + // We emit inferred type as type-alias at the current localtion if all the following is true + // the input type is has alias symbol that is accessible + // the input type is a union, intersection or anonymous type that is fully instantiated (if not we want to keep dive into) + // e.g.: export type Bar = () => [X, Y]; + // export type Foo = Bar; + // export const y = (x: Foo) => 1 // we want to emit as ...x: () => [any, string]) const typeArguments = type.aliasTypeArguments; writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); } From a7f9d73498c010f0282b3825b9687cf6f36bb3f4 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 28 Sep 2016 14:51:49 -0700 Subject: [PATCH 2/2] Add tests and baselines --- ...arationEmitTypeAliasWithTypeParameters1.js | 15 +++++++++++++ ...onEmitTypeAliasWithTypeParameters1.symbols | 20 +++++++++++++++++ ...tionEmitTypeAliasWithTypeParameters1.types | 22 +++++++++++++++++++ ...arationEmitTypeAliasWithTypeParameters1.ts | 5 +++++ 4 files changed, 62 insertions(+) create mode 100644 tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.js create mode 100644 tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.symbols create mode 100644 tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.types create mode 100644 tests/cases/compiler/declarationEmitTypeAliasWithTypeParameters1.ts diff --git a/tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.js b/tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.js new file mode 100644 index 00000000000..c41c4690391 --- /dev/null +++ b/tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.js @@ -0,0 +1,15 @@ +//// [declarationEmitTypeAliasWithTypeParameters1.ts] + +export type Bar = () => [X, Y]; +export type Foo = Bar; +export const y = (x: Foo) => 1 + +//// [declarationEmitTypeAliasWithTypeParameters1.js] +"use strict"; +exports.y = function (x) { return 1; }; + + +//// [declarationEmitTypeAliasWithTypeParameters1.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/declarationEmitTypeAliasWithTypeParameters1.symbols b/tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.symbols new file mode 100644 index 00000000000..fa63e711f9a --- /dev/null +++ b/tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.symbols @@ -0,0 +1,20 @@ +=== tests/cases/compiler/declarationEmitTypeAliasWithTypeParameters1.ts === + +export type Bar = () => [X, Y]; +>Bar : Symbol(Bar, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 0, 0)) +>X : Symbol(X, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 16)) +>Y : Symbol(Y, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 18)) +>X : Symbol(X, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 16)) +>Y : Symbol(Y, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 18)) + +export type Foo = Bar; +>Foo : Symbol(Foo, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 37)) +>Y : Symbol(Y, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 2, 16)) +>Bar : Symbol(Bar, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 0, 0)) +>Y : Symbol(Y, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 2, 16)) + +export const y = (x: Foo) => 1 +>y : Symbol(y, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 3, 12)) +>x : Symbol(x, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 3, 18)) +>Foo : Symbol(Foo, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 37)) + diff --git a/tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.types b/tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.types new file mode 100644 index 00000000000..3ff9f9bde1d --- /dev/null +++ b/tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.types @@ -0,0 +1,22 @@ +=== tests/cases/compiler/declarationEmitTypeAliasWithTypeParameters1.ts === + +export type Bar = () => [X, Y]; +>Bar : Bar +>X : X +>Y : Y +>X : X +>Y : Y + +export type Foo = Bar; +>Foo : () => [any, Y] +>Y : Y +>Bar : Bar +>Y : Y + +export const y = (x: Foo) => 1 +>y : (x: () => [any, string]) => number +>(x: Foo) => 1 : (x: () => [any, string]) => number +>x : () => [any, string] +>Foo : () => [any, Y] +>1 : 1 + diff --git a/tests/cases/compiler/declarationEmitTypeAliasWithTypeParameters1.ts b/tests/cases/compiler/declarationEmitTypeAliasWithTypeParameters1.ts new file mode 100644 index 00000000000..ff9042af699 --- /dev/null +++ b/tests/cases/compiler/declarationEmitTypeAliasWithTypeParameters1.ts @@ -0,0 +1,5 @@ +// @declaration: true + +export type Bar = () => [X, Y]; +export type Foo = Bar; +export const y = (x: Foo) => 1 \ No newline at end of file