Skip to content

Error on type inference inside an IF and arrow function when using --scrictNullChecks #19606

Description

TypeScript Version: 2.7.0-dev.20171031

Code

declarefunctionf(a: string,c: string): void;functionf2(a?: string,b?: string[]): void{if(a&&b){b.forEach(//b: string[]c=>f(a,c)//a: string (OK));}}functionf3(a: string|undefined,b: string[]|undefined): void{if(a&&b){b.forEach(//b: string[]c=>f(a,c)//a: string (OK));}}functionf4(x: any): void{leta: string|undefined=x.getA();letb: string[]|undefined=x.getB();if(a&&b){b.forEach(//b: string[]c=>f(a,c)//a: string|undefined (should be just string));}}functionf5(x: any): void{leta: string|undefined=x.getA();letb: string[]|undefined=x.getB();if(a&&b){f(a,b[0])//a: string (OK)}}functionf6(x: any): void{leta: string|undefined=x.getA();letb: string[]|undefined=x.getB();if(a){letd: string=a.toLowerCase();//a: string (OK)if(b){b.forEach(//b: string[]c=>f(a,c)//a: string|undefined (should be just string));}}}

Expected behavior:
No error, a: string|undefined should be string inside a if (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'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions