Closed
Description
Bug Report
π Search Terms
Type predicate, mapped types, narrowing
π Version & Regression Information
Tried with v4.3.5
β― Playground Link
π» Code
declare const areValuesDefined:
<T>(t: T) => t is { [K in keyof T]: Exclude<T[K], undefined> }
let x = {} as { a: string | undefined } | { b: string }
if (areValuesDefined(x)) {
x;
// Expected: { a: string } | { b: string }
// Actual: { b: string }
}
π Actual behavior
The type of x
, inside the if
block, is { b: string }
π Expected behavior
The type of x
, inside the if
block, should have been { a: string } | { b: string }
.
Also the quickinfo of areValueDefined
is inconsistent with the what actually the narrowed type is. The quickinfo shows the expected type.
Also the narrowing { b: string }
is particularly weird, even if it somehow failed it should have kept the type as it is.
And the narrowing does work in this case...
declare const areValuesDefined:
<T>(t: T) => t is { [K in keyof T]: Exclude<T[K], undefined> }
let x = {} as { a: string | undefined } | { b: string | undefined }
if (areValuesDefined(x)) {
x;
// Expected: { a: string } | { b: string }
// Actual: { a: string } | { b: string }
}