Closed
Description
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
Playground link with relevant code
💻 Code
class Utils {
static isDefined<T>(value: T): value is NonNullable<T> {
return value != null;
};
}
class A {
public testNumber: number | undefined;
constructor() {
const isNumber = Utils.isDefined(this.testNumber);
if (isNumber) {
// Type 'number | undefined' is not assignable to type 'number'
const x: number = this.testNumber;
}
}
}
function foo(x: number | undefined) {
const isNum = Utils.isDefined(x);
if(isNum) {
// Works. No error here
const y: number = x;
}
}
🙁 Actual behavior
The narrowed type of class property is not inferred if narrowing result stored in variable
I expect the type to be correctly inferred as it's working for regular variables inside a function
This PR fixed this bug for functions