Skip to content

Union type passes check when each of its members fail. #32398

@sbrow

Description

@sbrow

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:
union type
bug

Code

class Package {
    public name: string;
    public isDev?: boolean;
}

interface Devable extends Package {
    devOnly: false;
}

interface DevOnlyPackage extends Package {
    isDev?: true;
    devOnly: true;
}

// This should throw an error.
const d: Package | Devable | DevOnlyPackage = {
    name: "yarn",
    isDev: false,
    devOnly: true,
};

Expected behavior:
The annotation on const d should throw an error. It can't be a Package because it has additional fields, it can't be Devable because devOnly is false, and it can't be DevOnlyPackage because isDev is false.

Actual behavior:
No error is thrown.

Playground Link: playground

If you try to assert each type individually, it will throw an error as expected, but when a union type is used, it passes the check.

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions