From 03c11c8f70ac8f6122eed25ee28e5886a4db8f66 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 28 Feb 2022 13:41:10 -0800 Subject: [PATCH] Add missing `getReducedType` call in `getConditionalTypeInstantiation` (#48061) --- src/compiler/checker.ts | 2 +- .../objectAssignLikeNonUnionResult.js | 25 ++++++++ .../objectAssignLikeNonUnionResult.symbols | 64 +++++++++++++++++++ .../objectAssignLikeNonUnionResult.types | 53 +++++++++++++++ .../objectAssignLikeNonUnionResult.ts | 18 ++++++ 5 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/objectAssignLikeNonUnionResult.js create mode 100644 tests/baselines/reference/objectAssignLikeNonUnionResult.symbols create mode 100644 tests/baselines/reference/objectAssignLikeNonUnionResult.types create mode 100644 tests/cases/compiler/objectAssignLikeNonUnionResult.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7079ecf8a48..1dc7af4c954 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16854,7 +16854,7 @@ namespace ts { // distributive conditional type T extends U ? X : Y is instantiated with A | B for T, the // result is (A extends U ? X : Y) | (B extends U ? X : Y). result = distributionType && checkType !== distributionType && distributionType.flags & (TypeFlags.Union | TypeFlags.Never) ? - mapTypeWithAlias(distributionType, t => getConditionalType(root, prependTypeMapping(checkType, t, newMapper)), aliasSymbol, aliasTypeArguments) : + mapTypeWithAlias(getReducedType(distributionType), t => getConditionalType(root, prependTypeMapping(checkType, t, newMapper)), aliasSymbol, aliasTypeArguments) : getConditionalType(root, newMapper, aliasSymbol, aliasTypeArguments); root.instantiations!.set(id, result); } diff --git a/tests/baselines/reference/objectAssignLikeNonUnionResult.js b/tests/baselines/reference/objectAssignLikeNonUnionResult.js new file mode 100644 index 00000000000..f39d6fc68c7 --- /dev/null +++ b/tests/baselines/reference/objectAssignLikeNonUnionResult.js @@ -0,0 +1,25 @@ +//// [objectAssignLikeNonUnionResult.ts] +interface Interface { + field: number; +} +const defaultValue: Interface = { field: 1 }; + +declare function assign(target: T, source: U): T & U; + +// Displayed type: Interface & { field: number } +// Underlying type: Something else... +const data1 = assign(defaultValue, Date.now() > 3 ? { field: 2 } : {}); + +type ExtractRawComponent = T extends { __raw: infer C } ? [L1: T, L2: C] : [R1: T]; +type t1 = ExtractRawComponent; + +// ??? +type Explode = T extends { x: infer A } ? [A] : 'X'; +// 'X' | [unknown] -- why? +type e1 = Explode; + +//// [objectAssignLikeNonUnionResult.js] +var defaultValue = { field: 1 }; +// Displayed type: Interface & { field: number } +// Underlying type: Something else... +var data1 = assign(defaultValue, Date.now() > 3 ? { field: 2 } : {}); diff --git a/tests/baselines/reference/objectAssignLikeNonUnionResult.symbols b/tests/baselines/reference/objectAssignLikeNonUnionResult.symbols new file mode 100644 index 00000000000..4b7231fd1c3 --- /dev/null +++ b/tests/baselines/reference/objectAssignLikeNonUnionResult.symbols @@ -0,0 +1,64 @@ +=== tests/cases/compiler/objectAssignLikeNonUnionResult.ts === +interface Interface { +>Interface : Symbol(Interface, Decl(objectAssignLikeNonUnionResult.ts, 0, 0)) + + field: number; +>field : Symbol(Interface.field, Decl(objectAssignLikeNonUnionResult.ts, 0, 21)) +} +const defaultValue: Interface = { field: 1 }; +>defaultValue : Symbol(defaultValue, Decl(objectAssignLikeNonUnionResult.ts, 3, 5)) +>Interface : Symbol(Interface, Decl(objectAssignLikeNonUnionResult.ts, 0, 0)) +>field : Symbol(field, Decl(objectAssignLikeNonUnionResult.ts, 3, 33)) + +declare function assign(target: T, source: U): T & U; +>assign : Symbol(assign, Decl(objectAssignLikeNonUnionResult.ts, 3, 45)) +>T : Symbol(T, Decl(objectAssignLikeNonUnionResult.ts, 5, 24)) +>U : Symbol(U, Decl(objectAssignLikeNonUnionResult.ts, 5, 26)) +>target : Symbol(target, Decl(objectAssignLikeNonUnionResult.ts, 5, 30)) +>T : Symbol(T, Decl(objectAssignLikeNonUnionResult.ts, 5, 24)) +>source : Symbol(source, Decl(objectAssignLikeNonUnionResult.ts, 5, 40)) +>U : Symbol(U, Decl(objectAssignLikeNonUnionResult.ts, 5, 26)) +>T : Symbol(T, Decl(objectAssignLikeNonUnionResult.ts, 5, 24)) +>U : Symbol(U, Decl(objectAssignLikeNonUnionResult.ts, 5, 26)) + +// Displayed type: Interface & { field: number } +// Underlying type: Something else... +const data1 = assign(defaultValue, Date.now() > 3 ? { field: 2 } : {}); +>data1 : Symbol(data1, Decl(objectAssignLikeNonUnionResult.ts, 9, 5)) +>assign : Symbol(assign, Decl(objectAssignLikeNonUnionResult.ts, 3, 45)) +>defaultValue : Symbol(defaultValue, Decl(objectAssignLikeNonUnionResult.ts, 3, 5)) +>Date.now : Symbol(DateConstructor.now, Decl(lib.es5.d.ts, --, --)) +>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --)) +>now : Symbol(DateConstructor.now, Decl(lib.es5.d.ts, --, --)) +>field : Symbol(field, Decl(objectAssignLikeNonUnionResult.ts, 9, 53)) + +type ExtractRawComponent = T extends { __raw: infer C } ? [L1: T, L2: C] : [R1: T]; +>ExtractRawComponent : Symbol(ExtractRawComponent, Decl(objectAssignLikeNonUnionResult.ts, 9, 71)) +>T : Symbol(T, Decl(objectAssignLikeNonUnionResult.ts, 11, 25)) +>T : Symbol(T, Decl(objectAssignLikeNonUnionResult.ts, 11, 25)) +>__raw : Symbol(__raw, Decl(objectAssignLikeNonUnionResult.ts, 11, 41)) +>C : Symbol(C, Decl(objectAssignLikeNonUnionResult.ts, 11, 54)) +>T : Symbol(T, Decl(objectAssignLikeNonUnionResult.ts, 11, 25)) +>C : Symbol(C, Decl(objectAssignLikeNonUnionResult.ts, 11, 54)) +>T : Symbol(T, Decl(objectAssignLikeNonUnionResult.ts, 11, 25)) + +type t1 = ExtractRawComponent; +>t1 : Symbol(t1, Decl(objectAssignLikeNonUnionResult.ts, 11, 86)) +>ExtractRawComponent : Symbol(ExtractRawComponent, Decl(objectAssignLikeNonUnionResult.ts, 9, 71)) +>data1 : Symbol(data1, Decl(objectAssignLikeNonUnionResult.ts, 9, 5)) + +// ??? +type Explode = T extends { x: infer A } ? [A] : 'X'; +>Explode : Symbol(Explode, Decl(objectAssignLikeNonUnionResult.ts, 12, 44)) +>T : Symbol(T, Decl(objectAssignLikeNonUnionResult.ts, 15, 13)) +>T : Symbol(T, Decl(objectAssignLikeNonUnionResult.ts, 15, 13)) +>x : Symbol(x, Decl(objectAssignLikeNonUnionResult.ts, 15, 29)) +>A : Symbol(A, Decl(objectAssignLikeNonUnionResult.ts, 15, 38)) +>A : Symbol(A, Decl(objectAssignLikeNonUnionResult.ts, 15, 38)) + +// 'X' | [unknown] -- why? +type e1 = Explode; +>e1 : Symbol(e1, Decl(objectAssignLikeNonUnionResult.ts, 15, 55)) +>Explode : Symbol(Explode, Decl(objectAssignLikeNonUnionResult.ts, 12, 44)) +>data1 : Symbol(data1, Decl(objectAssignLikeNonUnionResult.ts, 9, 5)) + diff --git a/tests/baselines/reference/objectAssignLikeNonUnionResult.types b/tests/baselines/reference/objectAssignLikeNonUnionResult.types new file mode 100644 index 00000000000..7553d3e589c --- /dev/null +++ b/tests/baselines/reference/objectAssignLikeNonUnionResult.types @@ -0,0 +1,53 @@ +=== tests/cases/compiler/objectAssignLikeNonUnionResult.ts === +interface Interface { + field: number; +>field : number +} +const defaultValue: Interface = { field: 1 }; +>defaultValue : Interface +>{ field: 1 } : { field: number; } +>field : number +>1 : 1 + +declare function assign(target: T, source: U): T & U; +>assign : (target: T, source: U) => T & U +>target : T +>source : U + +// Displayed type: Interface & { field: number } +// Underlying type: Something else... +const data1 = assign(defaultValue, Date.now() > 3 ? { field: 2 } : {}); +>data1 : Interface & { field: number; } +>assign(defaultValue, Date.now() > 3 ? { field: 2 } : {}) : Interface & { field: number; } +>assign : (target: T, source: U) => T & U +>defaultValue : Interface +>Date.now() > 3 ? { field: 2 } : {} : { field: number; } | {} +>Date.now() > 3 : boolean +>Date.now() : number +>Date.now : () => number +>Date : DateConstructor +>now : () => number +>3 : 3 +>{ field: 2 } : { field: number; } +>field : number +>2 : 2 +>{} : {} + +type ExtractRawComponent = T extends { __raw: infer C } ? [L1: T, L2: C] : [R1: T]; +>ExtractRawComponent : ExtractRawComponent +>__raw : C + +type t1 = ExtractRawComponent; +>t1 : [R1: Interface & { field: number; }] +>data1 : Interface & { field: number; } + +// ??? +type Explode = T extends { x: infer A } ? [A] : 'X'; +>Explode : Explode +>x : A + +// 'X' | [unknown] -- why? +type e1 = Explode; +>e1 : "X" +>data1 : Interface & { field: number; } + diff --git a/tests/cases/compiler/objectAssignLikeNonUnionResult.ts b/tests/cases/compiler/objectAssignLikeNonUnionResult.ts new file mode 100644 index 00000000000..47f693a0470 --- /dev/null +++ b/tests/cases/compiler/objectAssignLikeNonUnionResult.ts @@ -0,0 +1,18 @@ +interface Interface { + field: number; +} +const defaultValue: Interface = { field: 1 }; + +declare function assign(target: T, source: U): T & U; + +// Displayed type: Interface & { field: number } +// Underlying type: Something else... +const data1 = assign(defaultValue, Date.now() > 3 ? { field: 2 } : {}); + +type ExtractRawComponent = T extends { __raw: infer C } ? [L1: T, L2: C] : [R1: T]; +type t1 = ExtractRawComponent; + +// ??? +type Explode = T extends { x: infer A } ? [A] : 'X'; +// 'X' | [unknown] -- why? +type e1 = Explode; \ No newline at end of file