From 4fb0f06f6da3958aada29c2306d3aa127055a1b1 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 5 Oct 2016 11:45:52 -0700 Subject: [PATCH] Serialize type alias when type alias symbol is not accessible --- src/compiler/checker.ts | 45 ++++++++++-------- src/compiler/declarationEmitter.ts | 10 ++-- src/compiler/types.ts | 3 +- .../controlFlowBinaryOrExpression.types | 12 ++--- .../declarationEmitInferedTypeAlias1.types | 12 ++--- .../declarationEmitInferedTypeAlias2.types | 18 ++++---- .../declarationEmitInferedTypeAlias3.types | 6 +-- .../declarationEmitInferedTypeAlias4.js | 19 ++++++++ .../declarationEmitInferedTypeAlias4.symbols | 22 +++++++++ .../declarationEmitInferedTypeAlias4.types | 22 +++++++++ .../declarationEmitInferedTypeAlias5.js | 29 ++++++++++++ .../declarationEmitInferedTypeAlias5.symbols | 20 ++++++++ .../declarationEmitInferedTypeAlias5.types | 24 ++++++++++ .../declarationEmitInferedTypeAlias6.js | 30 ++++++++++++ .../declarationEmitInferedTypeAlias6.symbols | 19 ++++++++ .../declarationEmitInferedTypeAlias6.types | 23 ++++++++++ .../declarationEmitInferedTypeAlias7.js | 25 ++++++++++ .../declarationEmitInferedTypeAlias7.symbols | 16 +++++++ .../declarationEmitInferedTypeAlias7.types | 20 ++++++++ .../declarationEmitInferedTypeAlias8.js | 22 +++++++++ .../declarationEmitInferedTypeAlias8.symbols | 20 ++++++++ .../declarationEmitInferedTypeAlias8.types | 20 ++++++++ .../declarationEmitInferedTypeAlias9.js | 22 +++++++++ .../declarationEmitInferedTypeAlias9.symbols | 20 ++++++++ .../declarationEmitInferedTypeAlias9.types | 20 ++++++++ .../reference/genericTypeAliases.types | 46 +++++++++---------- .../stringLiteralTypesAndTuples01.js | 2 +- .../stringLiteralTypesOverloads01.js | 2 +- ...unusedLocalsAndParametersTypeAliases.types | 32 ++++++------- 29 files changed, 490 insertions(+), 91 deletions(-) create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias4.js create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias4.symbols create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias4.types create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias5.js create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias5.symbols create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias5.types create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias6.js create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias6.symbols create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias6.types create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias7.js create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias7.symbols create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias7.types create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias8.js create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias8.symbols create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias8.types create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias9.js create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias9.symbols create mode 100644 tests/baselines/reference/declarationEmitInferedTypeAlias9.types diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0ba65d6e61a..9bddb8d3e81 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1744,7 +1744,15 @@ namespace ts { return false; } - function isSymbolAccessible(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags): SymbolAccessibilityResult { + /** + * Check if the given symbol in given enclosing declaration is accessible and mark all associated alias to be visible if requested + * + * @param symbol a Symbol to check if accessible + * @param enclosingDeclaration a Node containing the symbol + * @param meaning a SymbolFlags to check if such meaning of the symbol is accessible + * @param shouldComputeAliasToMarkVisible a boolean value to indicate whether to return aliases to be mark visible in case the symbol is accessible + */ + function isSymbolAccessible(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags, shouldComputeAliasesToMakeVisible: boolean): SymbolAccessibilityResult { if (symbol && enclosingDeclaration && !(symbol.flags & SymbolFlags.TypeParameter)) { const initialSymbol = symbol; let meaningToLook = meaning; @@ -1752,7 +1760,7 @@ namespace ts { // Symbol is accessible if it by itself is accessible const accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaningToLook, /*useOnlyExternalAliasing*/ false); if (accessibleSymbolChain) { - const hasAccessibleDeclarations = hasVisibleDeclarations(accessibleSymbolChain[0]); + const hasAccessibleDeclarations = hasVisibleDeclarations(accessibleSymbolChain[0], shouldComputeAliasesToMakeVisible); if (!hasAccessibleDeclarations) { return { accessibility: SymbolAccessibility.NotAccessible, @@ -1816,7 +1824,7 @@ namespace ts { return isAmbientModule(declaration) || (declaration.kind === SyntaxKind.SourceFile && isExternalOrCommonJsModule(declaration)); } - function hasVisibleDeclarations(symbol: Symbol): SymbolVisibilityResult { + function hasVisibleDeclarations(symbol: Symbol, shouldComputeAliasToMarkVisible: boolean): SymbolVisibilityResult { let aliasesToMakeVisible: AnyImportSyntax[]; if (forEach(symbol.declarations, declaration => !getIsDeclarationVisible(declaration))) { return undefined; @@ -1832,14 +1840,16 @@ namespace ts { if (anyImportSyntax && !(anyImportSyntax.flags & NodeFlags.Export) && // import clause without export isDeclarationVisible(anyImportSyntax.parent)) { - getNodeLinks(declaration).isVisible = true; - if (aliasesToMakeVisible) { - if (!contains(aliasesToMakeVisible, anyImportSyntax)) { - aliasesToMakeVisible.push(anyImportSyntax); + if (shouldComputeAliasToMarkVisible) { + getNodeLinks(declaration).isVisible = true; + if (aliasesToMakeVisible) { + if (!contains(aliasesToMakeVisible, anyImportSyntax)) { + aliasesToMakeVisible.push(anyImportSyntax); + } + } + else { + aliasesToMakeVisible = [anyImportSyntax]; } - } - else { - aliasesToMakeVisible = [anyImportSyntax]; } return true; } @@ -1874,7 +1884,7 @@ namespace ts { const symbol = resolveName(enclosingDeclaration, (firstIdentifier).text, meaning, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); // Verify if the symbol is accessible - return (symbol && hasVisibleDeclarations(symbol)) || { + return (symbol && hasVisibleDeclarations(symbol, /*shouldComputeAliasToMarkVisible*/ true)) || { accessibility: SymbolAccessibility.NotAccessible, errorSymbolName: getTextOfNode(firstIdentifier), errorNode: firstIdentifier @@ -2152,14 +2162,11 @@ 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 && + 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 + 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/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 5db6cd72bfb..c4f945000fa 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -306,7 +306,7 @@ namespace ts { } function trackSymbol(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags) { - handleSymbolAccessibilityError(resolver.isSymbolAccessible(symbol, enclosingDeclaration, meaning)); + handleSymbolAccessibilityError(resolver.isSymbolAccessible(symbol, enclosingDeclaration, meaning, /*shouldComputeAliasesToMakeVisible*/ true)); recordTypeReferenceDirectivesIfNecessary(resolver.getTypeReferenceDirectivesForSymbol(symbol, meaning)); } @@ -327,7 +327,7 @@ namespace ts { } else { errorNameNode = declaration.name; - resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, TypeFormatFlags.UseTypeOfFunction | TypeFormatFlags.UseTypeAliasValue, writer); + resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, TypeFormatFlags.UseTypeOfFunction, writer); errorNameNode = undefined; } } @@ -341,7 +341,7 @@ namespace ts { } else { errorNameNode = signature.name; - resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, TypeFormatFlags.UseTypeOfFunction | TypeFormatFlags.UseTypeAliasValue, writer); + resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, TypeFormatFlags.UseTypeOfFunction, writer); errorNameNode = undefined; } } @@ -563,7 +563,7 @@ namespace ts { write(tempVarName); write(": "); writer.getSymbolAccessibilityDiagnostic = getDefaultExportAccessibilityDiagnostic; - resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, TypeFormatFlags.UseTypeOfFunction | TypeFormatFlags.UseTypeAliasValue, writer); + resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, TypeFormatFlags.UseTypeOfFunction, writer); write(";"); writeLine(); write(node.isExportEquals ? "export = " : "export default "); @@ -1025,7 +1025,7 @@ namespace ts { } else { writer.getSymbolAccessibilityDiagnostic = getHeritageClauseVisibilityError; - resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, TypeFormatFlags.UseTypeOfFunction | TypeFormatFlags.UseTypeAliasValue, writer); + resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, TypeFormatFlags.UseTypeOfFunction, writer); } function getHeritageClauseVisibilityError(symbolAccessibilityResult: SymbolAccessibilityResult): SymbolAccessibilityDiagnostic { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 5742ff319e3..10bf9017891 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1949,7 +1949,6 @@ namespace ts { UseFullyQualifiedType = 0x00000080, // Write out the fully qualified type name (eg. Module.Type, instead of Type) InFirstTypeArgument = 0x00000100, // Writing first type argument of the instantiated type InTypeAlias = 0x00000200, // Writing type in type alias declaration - UseTypeAliasValue = 0x00000400, // Serialize the type instead of using type-alias. This is needed when we emit declaration file. } export const enum SymbolFormatFlags { @@ -2052,7 +2051,7 @@ namespace ts { writeReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; writeBaseConstructorTypeOfClass(node: ClassLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void; - isSymbolAccessible(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags): SymbolAccessibilityResult; + isSymbolAccessible(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags, shouldComputeAliasToMarkVisible: boolean): SymbolAccessibilityResult; isEntityNameVisible(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node): SymbolVisibilityResult; // Returns the constant value this property access resolves to, or 'undefined' for a non-constant getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): number; diff --git a/tests/baselines/reference/controlFlowBinaryOrExpression.types b/tests/baselines/reference/controlFlowBinaryOrExpression.types index 7e92fcdbbd1..40d1db4d808 100644 --- a/tests/baselines/reference/controlFlowBinaryOrExpression.types +++ b/tests/baselines/reference/controlFlowBinaryOrExpression.types @@ -62,21 +62,21 @@ declare function isHTMLCollection(sourceObj: any): sourceObj is HTMLCollection; >HTMLCollection : HTMLCollection type EventTargetLike = {a: string} | HTMLCollection | NodeList; ->EventTargetLike : EventTargetLike +>EventTargetLike : NodeList | HTMLCollection | { a: string; } >a : string >HTMLCollection : HTMLCollection >NodeList : NodeList var sourceObj: EventTargetLike = undefined; ->sourceObj : EventTargetLike ->EventTargetLike : EventTargetLike +>sourceObj : NodeList | HTMLCollection | { a: string; } +>EventTargetLike : NodeList | HTMLCollection | { a: string; } >undefined : any >undefined : undefined if (isNodeList(sourceObj)) { >isNodeList(sourceObj) : boolean >isNodeList : (sourceObj: any) => sourceObj is NodeList ->sourceObj : EventTargetLike +>sourceObj : NodeList | HTMLCollection | { a: string; } sourceObj.length; >sourceObj.length : number @@ -87,7 +87,7 @@ if (isNodeList(sourceObj)) { if (isHTMLCollection(sourceObj)) { >isHTMLCollection(sourceObj) : boolean >isHTMLCollection : (sourceObj: any) => sourceObj is HTMLCollection ->sourceObj : EventTargetLike +>sourceObj : NodeList | HTMLCollection | { a: string; } sourceObj.length; >sourceObj.length : number @@ -99,7 +99,7 @@ if (isNodeList(sourceObj) || isHTMLCollection(sourceObj)) { >isNodeList(sourceObj) || isHTMLCollection(sourceObj) : boolean >isNodeList(sourceObj) : boolean >isNodeList : (sourceObj: any) => sourceObj is NodeList ->sourceObj : EventTargetLike +>sourceObj : NodeList | HTMLCollection | { a: string; } >isHTMLCollection(sourceObj) : boolean >isHTMLCollection : (sourceObj: any) => sourceObj is HTMLCollection >sourceObj : HTMLCollection | { a: string; } diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias1.types b/tests/baselines/reference/declarationEmitInferedTypeAlias1.types index 8da9e8b291e..bdebedf3414 100644 --- a/tests/baselines/reference/declarationEmitInferedTypeAlias1.types +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias1.types @@ -2,22 +2,22 @@ { type Data = string | boolean; ->Data : Data +>Data : string | boolean let obj: Data = true; ->obj : Data ->Data : Data +>obj : string | boolean +>Data : string | boolean >true : true } export { } === tests/cases/compiler/1.ts === let v = "str" || true; ->v : Data ->"str" || true : Data +>v : string | boolean +>"str" || true : string | boolean >"str" : string >true : boolean export { v } ->v : Data +>v : string | boolean diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias2.types b/tests/baselines/reference/declarationEmitInferedTypeAlias2.types index fb2a042260d..33e859d42ac 100644 --- a/tests/baselines/reference/declarationEmitInferedTypeAlias2.types +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias2.types @@ -2,29 +2,29 @@ { type Data = string | boolean; ->Data : Data +>Data : string | boolean let obj: Data = true; ->obj : Data ->Data : Data +>obj : string | boolean +>Data : string | boolean >true : true } export { } === tests/cases/compiler/1.ts === let v = "str" || true; ->v : Data ->"str" || true : Data +>v : string | boolean +>"str" || true : string | boolean >"str" : string >true : boolean function bar () { ->bar : () => Data +>bar : () => string | boolean return v; ->v : Data +>v : string | boolean } export { v, bar } ->v : Data ->bar : () => Data +>v : string | boolean +>bar : () => string | boolean diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias3.types b/tests/baselines/reference/declarationEmitInferedTypeAlias3.types index 52c8f7fea2f..7b15ec77165 100644 --- a/tests/baselines/reference/declarationEmitInferedTypeAlias3.types +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias3.types @@ -2,11 +2,11 @@ { type Data = string | boolean; ->Data : Data +>Data : string | boolean let obj: Data = true; ->obj : Data ->Data : Data +>obj : string | boolean +>Data : string | boolean >true : true } export { } diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias4.js b/tests/baselines/reference/declarationEmitInferedTypeAlias4.js new file mode 100644 index 00000000000..8d7ef72012c --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias4.js @@ -0,0 +1,19 @@ +//// [declarationEmitInferedTypeAlias4.ts] + +function f() { + type Foo = T | { x: Foo }; + var x: Foo; + return x; +} + +//// [declarationEmitInferedTypeAlias4.js] +function f() { + var x; + return x; +} + + +//// [declarationEmitInferedTypeAlias4.d.ts] +declare function f(): A[] | { + x: A[] | any; +}; diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias4.symbols b/tests/baselines/reference/declarationEmitInferedTypeAlias4.symbols new file mode 100644 index 00000000000..f522eca7d0b --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias4.symbols @@ -0,0 +1,22 @@ +=== tests/cases/compiler/declarationEmitInferedTypeAlias4.ts === + +function f() { +>f : Symbol(f, Decl(declarationEmitInferedTypeAlias4.ts, 0, 0)) +>A : Symbol(A, Decl(declarationEmitInferedTypeAlias4.ts, 1, 11)) + + type Foo = T | { x: Foo }; +>Foo : Symbol(Foo, Decl(declarationEmitInferedTypeAlias4.ts, 1, 17)) +>T : Symbol(T, Decl(declarationEmitInferedTypeAlias4.ts, 2, 13)) +>T : Symbol(T, Decl(declarationEmitInferedTypeAlias4.ts, 2, 13)) +>x : Symbol(x, Decl(declarationEmitInferedTypeAlias4.ts, 2, 23)) +>Foo : Symbol(Foo, Decl(declarationEmitInferedTypeAlias4.ts, 1, 17)) +>T : Symbol(T, Decl(declarationEmitInferedTypeAlias4.ts, 2, 13)) + + var x: Foo; +>x : Symbol(x, Decl(declarationEmitInferedTypeAlias4.ts, 3, 7)) +>Foo : Symbol(Foo, Decl(declarationEmitInferedTypeAlias4.ts, 1, 17)) +>A : Symbol(A, Decl(declarationEmitInferedTypeAlias4.ts, 1, 11)) + + return x; +>x : Symbol(x, Decl(declarationEmitInferedTypeAlias4.ts, 3, 7)) +} diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias4.types b/tests/baselines/reference/declarationEmitInferedTypeAlias4.types new file mode 100644 index 00000000000..f4cc0789f45 --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias4.types @@ -0,0 +1,22 @@ +=== tests/cases/compiler/declarationEmitInferedTypeAlias4.ts === + +function f() { +>f : () => A[] | { x: A[] | any; } +>A : A + + type Foo = T | { x: Foo }; +>Foo : T | { x: T | any; } +>T : T +>T : T +>x : T | { x: T | any; } +>Foo : T | { x: T | any; } +>T : T + + var x: Foo; +>x : A[] | { x: A[] | any; } +>Foo : T | { x: T | any; } +>A : A + + return x; +>x : A[] | { x: A[] | any; } +} diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias5.js b/tests/baselines/reference/declarationEmitInferedTypeAlias5.js new file mode 100644 index 00000000000..79d28d3d181 --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias5.js @@ -0,0 +1,29 @@ +//// [tests/cases/compiler/declarationEmitInferedTypeAlias5.ts] //// + +//// [0.ts] + +export type Data = string | boolean; +let obj: Data = true; + +//// [1.ts] +import * as Z from "./0" +//let v2: Z.Data; +let v = "str" || true; +export { v } + +//// [0.js] +"use strict"; +var obj = true; +//// [1.js] +"use strict"; +//let v2: Z.Data; +var v = "str" || true; +exports.v = v; + + +//// [0.d.ts] +export declare type Data = string | boolean; +//// [1.d.ts] +import * as Z from "./0"; +declare let v: Z.Data; +export { v }; diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias5.symbols b/tests/baselines/reference/declarationEmitInferedTypeAlias5.symbols new file mode 100644 index 00000000000..6c06697cfdc --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias5.symbols @@ -0,0 +1,20 @@ +=== tests/cases/compiler/0.ts === + +export type Data = string | boolean; +>Data : Symbol(Data, Decl(0.ts, 0, 0)) + +let obj: Data = true; +>obj : Symbol(obj, Decl(0.ts, 2, 3)) +>Data : Symbol(Data, Decl(0.ts, 0, 0)) + +=== tests/cases/compiler/1.ts === +import * as Z from "./0" +>Z : Symbol(Z, Decl(1.ts, 0, 6)) + +//let v2: Z.Data; +let v = "str" || true; +>v : Symbol(v, Decl(1.ts, 2, 3)) + +export { v } +>v : Symbol(v, Decl(1.ts, 3, 8)) + diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias5.types b/tests/baselines/reference/declarationEmitInferedTypeAlias5.types new file mode 100644 index 00000000000..d2f3fa63a71 --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias5.types @@ -0,0 +1,24 @@ +=== tests/cases/compiler/0.ts === + +export type Data = string | boolean; +>Data : Data + +let obj: Data = true; +>obj : Data +>Data : Data +>true : true + +=== tests/cases/compiler/1.ts === +import * as Z from "./0" +>Z : typeof Z + +//let v2: Z.Data; +let v = "str" || true; +>v : Z.Data +>"str" || true : Z.Data +>"str" : string +>true : boolean + +export { v } +>v : Z.Data + diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias6.js b/tests/baselines/reference/declarationEmitInferedTypeAlias6.js new file mode 100644 index 00000000000..924f73d0ed9 --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias6.js @@ -0,0 +1,30 @@ +//// [tests/cases/compiler/declarationEmitInferedTypeAlias6.ts] //// + +//// [0.ts] + +{ + type Data = string | boolean; + let obj: Data = true; +} +export { } + +//// [1.ts] +let v = "str" || true; +export { v } + +//// [0.js] +"use strict"; +{ + var obj = true; +} +//// [1.js] +"use strict"; +var v = "str" || true; +exports.v = v; + + +//// [0.d.ts] +export { }; +//// [1.d.ts] +declare let v: string | boolean; +export { v }; diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias6.symbols b/tests/baselines/reference/declarationEmitInferedTypeAlias6.symbols new file mode 100644 index 00000000000..94599418c5d --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias6.symbols @@ -0,0 +1,19 @@ +=== tests/cases/compiler/0.ts === + +{ + type Data = string | boolean; +>Data : Symbol(Data, Decl(0.ts, 1, 1)) + + let obj: Data = true; +>obj : Symbol(obj, Decl(0.ts, 3, 7)) +>Data : Symbol(Data, Decl(0.ts, 1, 1)) +} +export { } + +=== tests/cases/compiler/1.ts === +let v = "str" || true; +>v : Symbol(v, Decl(1.ts, 0, 3)) + +export { v } +>v : Symbol(v, Decl(1.ts, 1, 8)) + diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias6.types b/tests/baselines/reference/declarationEmitInferedTypeAlias6.types new file mode 100644 index 00000000000..bdebedf3414 --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias6.types @@ -0,0 +1,23 @@ +=== tests/cases/compiler/0.ts === + +{ + type Data = string | boolean; +>Data : string | boolean + + let obj: Data = true; +>obj : string | boolean +>Data : string | boolean +>true : true +} +export { } + +=== tests/cases/compiler/1.ts === +let v = "str" || true; +>v : string | boolean +>"str" || true : string | boolean +>"str" : string +>true : boolean + +export { v } +>v : string | boolean + diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias7.js b/tests/baselines/reference/declarationEmitInferedTypeAlias7.js new file mode 100644 index 00000000000..2fba057fc91 --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias7.js @@ -0,0 +1,25 @@ +//// [tests/cases/compiler/declarationEmitInferedTypeAlias7.ts] //// + +//// [0.ts] + +export type Data = string | boolean; +let obj: Data = true; + +//// [1.ts] +let v = "str" || true; +export { v } + +//// [0.js] +"use strict"; +var obj = true; +//// [1.js] +"use strict"; +var v = "str" || true; +exports.v = v; + + +//// [0.d.ts] +export declare type Data = string | boolean; +//// [1.d.ts] +declare let v: string | boolean; +export { v }; diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias7.symbols b/tests/baselines/reference/declarationEmitInferedTypeAlias7.symbols new file mode 100644 index 00000000000..8923fd72692 --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias7.symbols @@ -0,0 +1,16 @@ +=== tests/cases/compiler/0.ts === + +export type Data = string | boolean; +>Data : Symbol(Data, Decl(0.ts, 0, 0)) + +let obj: Data = true; +>obj : Symbol(obj, Decl(0.ts, 2, 3)) +>Data : Symbol(Data, Decl(0.ts, 0, 0)) + +=== tests/cases/compiler/1.ts === +let v = "str" || true; +>v : Symbol(v, Decl(1.ts, 0, 3)) + +export { v } +>v : Symbol(v, Decl(1.ts, 1, 8)) + diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias7.types b/tests/baselines/reference/declarationEmitInferedTypeAlias7.types new file mode 100644 index 00000000000..dd59a513275 --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias7.types @@ -0,0 +1,20 @@ +=== tests/cases/compiler/0.ts === + +export type Data = string | boolean; +>Data : Data + +let obj: Data = true; +>obj : Data +>Data : Data +>true : true + +=== tests/cases/compiler/1.ts === +let v = "str" || true; +>v : string | boolean +>"str" || true : string | boolean +>"str" : string +>true : boolean + +export { v } +>v : string | boolean + diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias8.js b/tests/baselines/reference/declarationEmitInferedTypeAlias8.js new file mode 100644 index 00000000000..54a8ba37af1 --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias8.js @@ -0,0 +1,22 @@ +//// [declarationEmitInferedTypeAlias8.ts] + +type Foo = T | { x: Foo }; +var x: Foo; + +function returnSomeGlobalValue() { + return x; +} + +//// [declarationEmitInferedTypeAlias8.js] +var x; +function returnSomeGlobalValue() { + return x; +} + + +//// [declarationEmitInferedTypeAlias8.d.ts] +declare type Foo = T | { + x: Foo; +}; +declare var x: Foo; +declare function returnSomeGlobalValue(): Foo; diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias8.symbols b/tests/baselines/reference/declarationEmitInferedTypeAlias8.symbols new file mode 100644 index 00000000000..9a0c0974e79 --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias8.symbols @@ -0,0 +1,20 @@ +=== tests/cases/compiler/declarationEmitInferedTypeAlias8.ts === + +type Foo = T | { x: Foo }; +>Foo : Symbol(Foo, Decl(declarationEmitInferedTypeAlias8.ts, 0, 0)) +>T : Symbol(T, Decl(declarationEmitInferedTypeAlias8.ts, 1, 9)) +>T : Symbol(T, Decl(declarationEmitInferedTypeAlias8.ts, 1, 9)) +>x : Symbol(x, Decl(declarationEmitInferedTypeAlias8.ts, 1, 19)) +>Foo : Symbol(Foo, Decl(declarationEmitInferedTypeAlias8.ts, 0, 0)) +>T : Symbol(T, Decl(declarationEmitInferedTypeAlias8.ts, 1, 9)) + +var x: Foo; +>x : Symbol(x, Decl(declarationEmitInferedTypeAlias8.ts, 2, 3)) +>Foo : Symbol(Foo, Decl(declarationEmitInferedTypeAlias8.ts, 0, 0)) + +function returnSomeGlobalValue() { +>returnSomeGlobalValue : Symbol(returnSomeGlobalValue, Decl(declarationEmitInferedTypeAlias8.ts, 2, 21)) + + return x; +>x : Symbol(x, Decl(declarationEmitInferedTypeAlias8.ts, 2, 3)) +} diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias8.types b/tests/baselines/reference/declarationEmitInferedTypeAlias8.types new file mode 100644 index 00000000000..9fc0aaf7016 --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias8.types @@ -0,0 +1,20 @@ +=== tests/cases/compiler/declarationEmitInferedTypeAlias8.ts === + +type Foo = T | { x: Foo }; +>Foo : Foo +>T : T +>T : T +>x : Foo +>Foo : Foo +>T : T + +var x: Foo; +>x : Foo +>Foo : Foo + +function returnSomeGlobalValue() { +>returnSomeGlobalValue : () => Foo + + return x; +>x : Foo +} diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias9.js b/tests/baselines/reference/declarationEmitInferedTypeAlias9.js new file mode 100644 index 00000000000..667acdff49c --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias9.js @@ -0,0 +1,22 @@ +//// [declarationEmitInferedTypeAlias9.ts] + +type Foo = T | { x: Foo }; +var x: Foo; + +export function returnSomeGlobalValue() { + return x; +} + +//// [declarationEmitInferedTypeAlias9.js] +"use strict"; +var x; +function returnSomeGlobalValue() { + return x; +} +exports.returnSomeGlobalValue = returnSomeGlobalValue; + + +//// [declarationEmitInferedTypeAlias9.d.ts] +export declare function returnSomeGlobalValue(): number[] | { + x: number[] | any; +}; diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias9.symbols b/tests/baselines/reference/declarationEmitInferedTypeAlias9.symbols new file mode 100644 index 00000000000..5057ef22c0b --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias9.symbols @@ -0,0 +1,20 @@ +=== tests/cases/compiler/declarationEmitInferedTypeAlias9.ts === + +type Foo = T | { x: Foo }; +>Foo : Symbol(Foo, Decl(declarationEmitInferedTypeAlias9.ts, 0, 0)) +>T : Symbol(T, Decl(declarationEmitInferedTypeAlias9.ts, 1, 9)) +>T : Symbol(T, Decl(declarationEmitInferedTypeAlias9.ts, 1, 9)) +>x : Symbol(x, Decl(declarationEmitInferedTypeAlias9.ts, 1, 19)) +>Foo : Symbol(Foo, Decl(declarationEmitInferedTypeAlias9.ts, 0, 0)) +>T : Symbol(T, Decl(declarationEmitInferedTypeAlias9.ts, 1, 9)) + +var x: Foo; +>x : Symbol(x, Decl(declarationEmitInferedTypeAlias9.ts, 2, 3)) +>Foo : Symbol(Foo, Decl(declarationEmitInferedTypeAlias9.ts, 0, 0)) + +export function returnSomeGlobalValue() { +>returnSomeGlobalValue : Symbol(returnSomeGlobalValue, Decl(declarationEmitInferedTypeAlias9.ts, 2, 21)) + + return x; +>x : Symbol(x, Decl(declarationEmitInferedTypeAlias9.ts, 2, 3)) +} diff --git a/tests/baselines/reference/declarationEmitInferedTypeAlias9.types b/tests/baselines/reference/declarationEmitInferedTypeAlias9.types new file mode 100644 index 00000000000..d4f61bf927a --- /dev/null +++ b/tests/baselines/reference/declarationEmitInferedTypeAlias9.types @@ -0,0 +1,20 @@ +=== tests/cases/compiler/declarationEmitInferedTypeAlias9.ts === + +type Foo = T | { x: Foo }; +>Foo : T | { x: T | any; } +>T : T +>T : T +>x : T | { x: T | any; } +>Foo : T | { x: T | any; } +>T : T + +var x: Foo; +>x : number[] | { x: number[] | any; } +>Foo : T | { x: T | any; } + +export function returnSomeGlobalValue() { +>returnSomeGlobalValue : () => number[] | { x: number[] | any; } + + return x; +>x : number[] | { x: number[] | any; } +} diff --git a/tests/baselines/reference/genericTypeAliases.types b/tests/baselines/reference/genericTypeAliases.types index 929ecf7d7e1..2485df21a54 100644 --- a/tests/baselines/reference/genericTypeAliases.types +++ b/tests/baselines/reference/genericTypeAliases.types @@ -215,60 +215,60 @@ p.tag = "test"; >"test" : string function f() { ->f : () => Foo +>f : () => A[] | { x: A[] | any; } >A : A type Foo = T | { x: Foo }; ->Foo : Foo +>Foo : T | { x: T | any; } >T : T >T : T ->x : Foo ->Foo : Foo +>x : T | { x: T | any; } +>Foo : T | { x: T | any; } >T : T var x: Foo; ->x : Foo ->Foo : Foo +>x : A[] | { x: A[] | any; } +>Foo : T | { x: T | any; } >A : A return x; ->x : Foo +>x : A[] | { x: A[] | any; } } function g() { ->g : () => Bar +>g : () => B[] | { x: B[] | any; } >B : B type Bar = U | { x: Bar }; ->Bar : Bar +>Bar : U | { x: U | any; } >U : U >U : U ->x : Bar ->Bar : Bar +>x : U | { x: U | any; } +>Bar : U | { x: U | any; } >U : U var x: Bar; ->x : Bar ->Bar : Bar +>x : B[] | { x: B[] | any; } +>Bar : U | { x: U | any; } >B : B return x; ->x : Bar +>x : B[] | { x: B[] | any; } } // Deeply instantiated generics var a = f(); ->a : Foo ->f() : Foo ->f : () => Foo +>a : string[] | { x: string[] | any; } +>f() : string[] | { x: string[] | any; } +>f : () => A[] | { x: A[] | any; } var b = g(); ->b : Bar ->g() : Bar ->g : () => Bar +>b : string[] | { x: string[] | any; } +>g() : string[] | { x: string[] | any; } +>g : () => B[] | { x: B[] | any; } a = b; ->a = b : Bar ->a : Foo ->b : Bar +>a = b : string[] | { x: string[] | any; } +>a : string[] | { x: string[] | any; } +>b : string[] | { x: string[] | any; } diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.js b/tests/baselines/reference/stringLiteralTypesAndTuples01.js index ae02d12429a..b887213c8f6 100644 --- a/tests/baselines/reference/stringLiteralTypesAndTuples01.js +++ b/tests/baselines/reference/stringLiteralTypesAndTuples01.js @@ -38,5 +38,5 @@ function rawr(dino) { //// [stringLiteralTypesAndTuples01.d.ts] declare let hello: string, brave: string, newish: string, world: string; declare type RexOrRaptor = "t-rex" | "raptor"; -declare let im: "I'm", a: "a", dinosaur: "t-rex" | "raptor"; +declare let im: "I'm", a: "a", dinosaur: RexOrRaptor; declare function rawr(dino: RexOrRaptor): string; diff --git a/tests/baselines/reference/stringLiteralTypesOverloads01.js b/tests/baselines/reference/stringLiteralTypesOverloads01.js index ef441a40985..dd050b5e464 100644 --- a/tests/baselines/reference/stringLiteralTypesOverloads01.js +++ b/tests/baselines/reference/stringLiteralTypesOverloads01.js @@ -109,6 +109,6 @@ declare const boolean: "boolean"; declare const stringOrNumber: "string" | "number"; declare const stringOrBoolean: "string" | "boolean"; declare const booleanOrNumber: "number" | "boolean"; -declare const stringOrBooleanOrNumber: "string" | "number" | "boolean"; +declare const stringOrBooleanOrNumber: PrimitiveName; declare namespace Consts2 { } diff --git a/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.types b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.types index 2695b9b429a..57d251d635a 100644 --- a/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.types +++ b/tests/baselines/reference/unusedLocalsAndParametersTypeAliases.types @@ -2,14 +2,14 @@ // used in a declaration type handler1 = () => void; ->handler1 : handler1 +>handler1 : () => void export interface I1 { >I1 : I1 getHandler: handler1; ->getHandler : handler1 ->handler1 : handler1 +>getHandler : () => void +>handler1 : () => void } // exported @@ -18,12 +18,12 @@ export type handler2 = () => void; // used in extends clause type handler3 = () => void; ->handler3 : handler3 +>handler3 : () => void export interface I3 { >I3 : I3 >T : T ->handler3 : handler3 +>handler3 : () => void getHandler: T; >getHandler : T @@ -32,32 +32,32 @@ export interface I3 { // used in another type alias declaration type handler4 = () => void; ->handler4 : handler4 +>handler4 : () => void type handler5 = handler4 | (()=>number); ->handler5 : handler5 ->handler4 : handler4 +>handler5 : (() => void) | (() => number) +>handler4 : () => void var x: handler5; ->x : handler5 ->handler5 : handler5 +>x : (() => void) | (() => number) +>handler5 : (() => void) | (() => number) x(); >x() : number | void ->x : handler5 +>x : (() => void) | (() => number) // used as type argument type handler6 = () => void; ->handler6 : handler6 +>handler6 : () => void var y: Array; ->y : handler6[] +>y : (() => void)[] >Array : T[] ->handler6 : handler6 +>handler6 : () => void y[0](); >y[0]() : void ->y[0] : handler6 ->y : handler6[] +>y[0] : () => void +>y : (() => void)[] >0 : number