From adf815da614ad273e8c792082c478a51e8557918 Mon Sep 17 00:00:00 2001 From: Titian Cernicova-Dragomir Date: Fri, 27 Sep 2024 11:06:02 +0100 Subject: [PATCH] Remove dry by fix of type widening in declaration files. --- src/compiler/checker.ts | 11 ++++------ .../reference/ambientConstLiterals.js | 4 ++-- .../reference/strictFunctionTypes1.js | 20 +------------------ .../stringLiteralTypesOverloads02.js | 8 ++++---- 4 files changed, 11 insertions(+), 32 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 63561cbe75577..adf95d3061b3f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6088,16 +6088,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { serializeTypeOfDeclaration(syntacticContext, declaration, symbol) { // Get type of the symbol if this is the valid symbol otherwise get type at location const context = syntacticContext as NodeBuilderContext; - let type; - if (symbol === undefined) { - symbol ??= getSymbolOfDeclaration(declaration); + symbol ??= getSymbolOfDeclaration(declaration); + let type = context.enclosingSymbolTypes?.get(getSymbolId(symbol)); + if (type === undefined) { type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature)) - ? instantiateType(getTypeOfSymbol(symbol), context.mapper) + ? instantiateType(getWidenedLiteralType(getTypeOfSymbol(symbol)), context.mapper) : errorType; } - else { - type = context.enclosingSymbolTypes?.get(getSymbolId(symbol)) ?? getTypeOfSymbol(symbol); - } const addUndefinedForParameter = declaration && (isParameter(declaration) || isJSDocParameterTag(declaration)) && requiresAddingImplicitUndefined(declaration, context.enclosingDeclaration); if (addUndefinedForParameter) { type = getOptionalType(type); diff --git a/tests/baselines/reference/ambientConstLiterals.js b/tests/baselines/reference/ambientConstLiterals.js index 230df0414e773..8b35d43487203 100644 --- a/tests/baselines/reference/ambientConstLiterals.js +++ b/tests/baselines/reference/ambientConstLiterals.js @@ -74,5 +74,5 @@ declare const c9: { declare const c10: number[]; declare const c11: string; declare const c12: number; -declare const c13: "abc" | "def"; -declare const c14: 123 | 456; +declare const c13: string; +declare const c14: number; diff --git a/tests/baselines/reference/strictFunctionTypes1.js b/tests/baselines/reference/strictFunctionTypes1.js index 903222409a6bc..25769522a4898 100644 --- a/tests/baselines/reference/strictFunctionTypes1.js +++ b/tests/baselines/reference/strictFunctionTypes1.js @@ -82,7 +82,7 @@ declare function fs(x: string): void; declare function fx(f: (x: "def") => void): void; declare const x1: (x: string) => void; declare const x2 = "abc"; -declare const x3: "def" | "abc"; +declare const x3: string; declare const x4: Func; declare const never: never; declare const x10: string; @@ -107,21 +107,3 @@ declare function coAndContraArray(value: T[], func: (t: T) => void): T[]; declare const t4: A[]; declare const t5: B[]; declare const t6: A[]; - - -!!!! File strictFunctionTypes1.d.ts differs from original emit in noCheck emit -//// [strictFunctionTypes1.d.ts] -=================================================================== ---- Expected The full check baseline -+++ Actual with noCheck set -@@ -9,9 +9,9 @@ - declare function fs(x: string): void; - declare function fx(f: (x: "def") => void): void; - declare const x1: (x: string) => void; - declare const x2 = "abc"; --declare const x3: "def" | "abc"; -+declare const x3: "abc" | "def"; - declare const x4: Func; - declare const never: never; - declare const x10: string; - declare const x11: "def"; diff --git a/tests/baselines/reference/stringLiteralTypesOverloads02.js b/tests/baselines/reference/stringLiteralTypesOverloads02.js index 216a42aec80e2..cf6e285b9049b 100644 --- a/tests/baselines/reference/stringLiteralTypesOverloads02.js +++ b/tests/baselines/reference/stringLiteralTypesOverloads02.js @@ -104,9 +104,9 @@ declare namespace Consts1 { declare const string = "string"; declare const number = "number"; 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 stringOrNumber: string; +declare const stringOrBoolean: string; +declare const booleanOrNumber: string; +declare const stringOrBooleanOrNumber: string; declare namespace Consts2 { }