Skip to content
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

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

Closed
captain-igloo opened this issue Dec 13, 2018 · 1 comment
Labels
Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis Domain: Mapped Types The issue relates to mapped types
Milestone

Comments

@captain-igloo
Copy link

captain-igloo commented Dec 13, 2018

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

@weswigham weswigham added Bug A bug in TypeScript Domain: Mapped Types The issue relates to mapped types Domain: Control Flow The issue relates to control flow analysis labels Dec 13, 2018
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 14, 2019
@jakebailey
Copy link
Member

This one was fixed back in #43183, but probably deserves a test (I didn't see any that were similar in the PR).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis Domain: Mapped Types The issue relates to mapped types
Projects
None yet
Development

No branches or pull requests

4 participants