Closed
Description
TypeScript Version: 3.3.0-dev.20181220
Search Terms: narrow union regression
Code
const f = (_a: string, _b: string): void => {};
interface A {
a?: string;
b?: string;
}
interface B {
a: string;
b: string;
}
type U = A | B;
const u: U = {} as any;
// u.b narrowed, but not a
// Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
// Type 'undefined' is not assignable to type 'string'.
u.a && u.b && f(u.a, u.b);
// Error: u.a narrowed, but not b
// Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
// Type 'undefined' is not assignable to type 'string'.
u.b && u.a && f(u.a, u.b);
// Works
typeof u.a === "string" && typeof u.b === "string" && f(u.a, u.b);
// Works
const a: A = {} as any;
a.b && a.a && f(a.a, a.b);
Expected behavior:
All forms narrow correctly and compile with strictNullChecks.
This works correctly in 3.1.6
Actual behavior:
Only non-union or explicitly checking typeof narrows correctly in >=3.2.
Related Issues: