Avoid subtype reduction when creating a union result in discriminateTypeByDiscriminableItems (#54052)

This commit is contained in:
Mateusz Burzyński
2023-06-27 20:55:46 +02:00
committed by GitHub
parent b61f24d0ec
commit a3773ec590
4 changed files with 87 additions and 1 deletions

View File

@@ -22743,7 +22743,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
}
const filtered = contains(include, Ternary.False) ? getUnionType(types.filter((_, i) => include[i])) : target;
const filtered = contains(include, Ternary.False) ? getUnionType(types.filter((_, i) => include[i]), UnionReduction.None) : target;
return filtered.flags & TypeFlags.Never ? target : filtered;
}

View File

@@ -0,0 +1,42 @@
//// [tests/cases/compiler/contextualTypeSelfReferencing.ts] ////
=== contextualTypeSelfReferencing.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/54048
type narrow<def> = def extends string
>narrow : Symbol(narrow, Decl(contextualTypeSelfReferencing.ts, 0, 0))
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
? def
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
: def extends [unknown, ...unknown[]]
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
? def
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
: {
[k in keyof def]: narrow<def[k]>;
>k : Symbol(k, Decl(contextualTypeSelfReferencing.ts, 7, 7))
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
>narrow : Symbol(narrow, Decl(contextualTypeSelfReferencing.ts, 0, 0))
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 2, 12))
>k : Symbol(k, Decl(contextualTypeSelfReferencing.ts, 7, 7))
};
declare const parse: <def>(def: narrow<def>) => def;
>parse : Symbol(parse, Decl(contextualTypeSelfReferencing.ts, 10, 13))
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 10, 22), Decl(contextualTypeSelfReferencing.ts, 10, 27))
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 10, 22), Decl(contextualTypeSelfReferencing.ts, 10, 27))
>narrow : Symbol(narrow, Decl(contextualTypeSelfReferencing.ts, 0, 0))
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 10, 22), Decl(contextualTypeSelfReferencing.ts, 10, 27))
>def : Symbol(def, Decl(contextualTypeSelfReferencing.ts, 10, 22), Decl(contextualTypeSelfReferencing.ts, 10, 27))
const result = parse([{ a: "foo" }]);
>result : Symbol(result, Decl(contextualTypeSelfReferencing.ts, 12, 5))
>parse : Symbol(parse, Decl(contextualTypeSelfReferencing.ts, 10, 13))
>a : Symbol(a, Decl(contextualTypeSelfReferencing.ts, 12, 23))

View File

@@ -0,0 +1,28 @@
//// [tests/cases/compiler/contextualTypeSelfReferencing.ts] ////
=== contextualTypeSelfReferencing.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/54048
type narrow<def> = def extends string
>narrow : narrow<def>
? def
: def extends [unknown, ...unknown[]]
? def
: {
[k in keyof def]: narrow<def[k]>;
};
declare const parse: <def>(def: narrow<def>) => def;
>parse : <def>(def: narrow<def>) => def
>def : narrow<def>
const result = parse([{ a: "foo" }]);
>result : [{ a: "foo"; }]
>parse([{ a: "foo" }]) : [{ a: "foo"; }]
>parse : <def>(def: narrow<def>) => def
>[{ a: "foo" }] : [{ a: "foo"; }]
>{ a: "foo" } : { a: "foo"; }
>a : "foo"
>"foo" : "foo"

View File

@@ -0,0 +1,16 @@
// @strict: true
// @noEmit: true
// repro from https://github.com/microsoft/TypeScript/issues/54048
type narrow<def> = def extends string
? def
: def extends [unknown, ...unknown[]]
? def
: {
[k in keyof def]: narrow<def[k]>;
};
declare const parse: <def>(def: narrow<def>) => def;
const result = parse([{ a: "foo" }]);