Skip to content

undefined not assignable error within "if" statement, when member of Readonly<T>#28997

@captain-igloo

Description

@captain-igloo

TypeScript Version: 3.3.0-dev.20181212

readonly if

Code

interfaceIProps{foo1?: string;foo2: string;}classFoo<P>{protectedprops: Readonly<P>;constructor(props: Readonly<P>){this.props=props;}}classBar<PextendsIProps>extendsFoo<P>{privatetest1(){const{ foo1, foo2 }=this.props;if(foo1){foo1.toUpperCase();// finethis.test2(foo1);// error "Type undefined is not assignable to type string"}this.test2(foo2);// fine}privatetest2(foo: string): void{}}

Expected behavior:
I expect the above to compile without error
Actual behavior:
There's an error - Type 'undefined' is not assignable to type 'string'

"foo1" above can't be undefined because it's within an "if (foo1){}".

Playground
link

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions