Closed
Description
Bug Report
🔎 Search Terms
Control-Flow-Analysis Coalescing
🕗 Version & Regression Information
- The compiler is reporting a type error incorrectly.
- This changed between versions 3.9.7 and 4.0.5.
- Prior to 4.0, the syntax
??=
wasn't supported. - It is unchanged through 4.4.0 nightly.
⏯ Playground Link
Playground link with relevant code
💻 Code
function f1(a?: {}) : {} {
const n = null ?? {};
a ??= n;
return a; // no problem
}
function f2(a?: {}) : {} {
a ??= (null ?? {});
return a; // Type '{} | undefined' is not assignable to type '{}'.
}
function identityFunction<T>(a: T): T {
return a;
}
function f3(a?: {}) : {} {
a ??= identityFunction(null ?? {});
return a; // also fine
}
🙁 Actual behavior
On line 9, a type error is reported: "Type '{} | undefined' is not assignable to type '{}'."
🙂 Expected behavior
I would expect that the code should pass type checking. Here's why.
1 . ??=
generally supports narrowing types, as shown in the playground link.
2. ??
generally supports nullability narrowing, as shown in the playground link.
3. There's nothing special about a RHS of a ??=
that contains a ??
.