-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Error on type inference inside an IF and arrow function when using --scrictNullChecks #19606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Looks like a duplicate of #9998 A simpler example: declare function getA(): string | undefined;
function g() {
let a: string | undefined = getA();
if (a) {
return ["1", "2"].map(x => a.toLowerCase() + x);
}
} The problem with closures is that we don't know whether it's called immediately or stored for later. If the closure were stored for later and |
Thanks... It would be nice if we could define in the function type parameter if it would execute sync or async: interface JQueryStatic {
ajax(url: string, async callback: (response: any) => void);
each(arr: any[], sync callback: (obj: any) =>void);
}
//or
interface JQueryStatic {
ajax(url: string, +callback: (response: any) => void);
each(arr: any[], -callback: (obj: any) =>void);
} |
That's #11498 |
@Andy-MS, in the case of class X {
readonly a: string | undefined;
m() {
if (this.a) {
this.a; // string - OK
() => this.a; // string|undefined - Shouldn't be string ?
}
}
} |
See "Mitigating with readonly fields" in #9998 |
TypeScript Version: 2.7.0-dev.20171031
Code
Expected behavior:
No error,
a: string|undefined
should bestring
inside aif (a && b)
Actual behavior:
error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.
The text was updated successfully, but these errors were encountered: