Skip to content

Commit 1ef6b24

Browse files
committed
Avoid subtype reduction when creating a union result in discriminateTypeByDiscriminableItems
1 parent 9a52f94 commit 1ef6b24

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22684,7 +22684,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2268422684
}
2268522685
}
2268622686
}
22687-
const filtered = contains(include, Ternary.False) ? getUnionType(types.filter((_, i) => include[i])) : target;
22687+
const filtered = contains(include, Ternary.False) ? getUnionType(types.filter((_, i) => include[i]), UnionReduction.None) : target;
2268822688
return filtered.flags & TypeFlags.Never ? target : filtered;
2268922689
}
2269022690

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=== tests/cases/compiler/contextualTypeSelfReferencing.ts ===
2+
// repro from https://github.com/microsoft/TypeScript/issues/54048
3+
4+
type narrow<def> = def extends string
5+
>narrow : Symbol(narrow, Decl(contextualTypeSelfReferencing.ts, 0, 0))
6+
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
7+
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
8+
9+
? def
10+
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
11+
12+
: def extends [unknown, ...unknown[]]
13+
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
14+
15+
? def
16+
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
17+
18+
: {
19+
[k in keyof def]: narrow<def[k]>;
20+
>k : Symbol(k, Decl(contextualTypeSelfReferencing.ts, 7, 7))
21+
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
22+
>narrow : Symbol(narrow, Decl(contextualTypeSelfReferencing.ts, 0, 0))
23+
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
24+
>k : Symbol(k, Decl(contextualTypeSelfReferencing.ts, 7, 7))
25+
26+
};
27+
28+
declare const parse: <def>(def: narrow<def>) => def;
29+
>parse : Symbol(parse, Decl(contextualTypeSelfReferencing.ts, 10, 13))
30+
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 10, 22), Decl(contextualTypeSelfReferencing.ts, 10, 27))
31+
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 10, 22), Decl(contextualTypeSelfReferencing.ts, 10, 27))
32+
>narrow : Symbol(narrow, Decl(contextualTypeSelfReferencing.ts, 0, 0))
33+
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 10, 22), Decl(contextualTypeSelfReferencing.ts, 10, 27))
34+
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 10, 22), Decl(contextualTypeSelfReferencing.ts, 10, 27))
35+
36+
const result = parse([{ a: "foo" }]);
37+
>result : Symbol(result, Decl(contextualTypeSelfReferencing.ts, 12, 5))
38+
>parse : Symbol(parse, Decl(contextualTypeSelfReferencing.ts, 10, 13))
39+
>a : Symbol(a, Decl(contextualTypeSelfReferencing.ts, 12, 23))
40+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/contextualTypeSelfReferencing.ts ===
2+
// repro from https://github.com/microsoft/TypeScript/issues/54048
3+
4+
type narrow<def> = def extends string
5+
>narrow : narrow<def>
6+
7+
? def
8+
: def extends [unknown, ...unknown[]]
9+
? def
10+
: {
11+
[k in keyof def]: narrow<def[k]>;
12+
};
13+
14+
declare const parse: <def>(def: narrow<def>) => def;
15+
>parse : <def>(def: narrow<def>) => def
16+
>def : narrow<def>
17+
18+
const result = parse([{ a: "foo" }]);
19+
>result : [{ a: "foo"; }]
20+
>parse([{ a: "foo" }]) : [{ a: "foo"; }]
21+
>parse : <def>(def: narrow<def>) => def
22+
>[{ a: "foo" }] : [{ a: "foo"; }]
23+
>{ a: "foo" } : { a: "foo"; }
24+
>a : "foo"
25+
>"foo" : "foo"
26+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// repro from https://github.com/microsoft/TypeScript/issues/54048
5+
6+
type narrow<def> = def extends string
7+
? def
8+
: def extends [unknown, ...unknown[]]
9+
? def
10+
: {
11+
[k in keyof def]: narrow<def[k]>;
12+
};
13+
14+
declare const parse: <def>(def: narrow<def>) => def;
15+
16+
const result = parse([{ a: "foo" }]);

0 commit comments

Comments
 (0)