Skip to content

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

Closed
@captain-igloo

Description

@captain-igloo

TypeScript Version: 3.3.0-dev.20181212

readonly if

Code

interface IProps {
    foo1?: string;
    foo2: string;
}

class Foo<P> {
    protected props: Readonly<P>;

    constructor(props: Readonly<P>) {
        this.props = props;
    }
}

class Bar<P extends IProps> extends Foo<P> {

    private test1() {
        const { foo1, foo2 } = this.props;

        if (foo1) {
            foo1.toUpperCase(); // fine
            this.test2(foo1); // error "Type undefined is not assignable to type string"
        }
        this.test2(foo2); // fine
    }

    private test2(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

    BugA bug in TypeScriptDomain: Control FlowThe issue relates to control flow analysisDomain: Mapped TypesThe issue relates to mapped types

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions