Skip to content

Commit a3773ec

Browse files
authored
Avoid subtype reduction when creating a union result in discriminateTypeByDiscriminableItems (#54052)
1 parent b61f24d commit a3773ec

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22743,7 +22743,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2274322743
}
2274422744
}
2274522745
}
22746-
const filtered = contains(include, Ternary.False) ? getUnionType(types.filter((_, i) => include[i])) : target;
22746+
const filtered = contains(include, Ternary.False) ? getUnionType(types.filter((_, i) => include[i]), UnionReduction.None) : target;
2274722747
return filtered.flags & TypeFlags.Never ? target : filtered;
2274822748
}
2274922749

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