Skip to content

Regression in discriminated unions when a common property may be boolean #32657

Closed
@janruo

Description

@janruo

TypeScript Version:
3.6.0-dev.20190801

Search Terms:
discriminated boolean, tagged boolean

Code

interface Base<T> {
    value: T;
}

interface Int extends Base<number> {
    type: "integer";
    multipleOf?: number;
}

interface Float extends Base<number> {
    type: "number";
}

interface Str extends Base<string> {
    type: "string";
    format?: string;
}

interface Bool extends Base<boolean> {
    type: "boolean";
}

type Primitive = Int | Float | Str | Bool;

const foo: Primitive = {
    type: "number",
    value: 10,
    multipleOf: 5,
    format: "what?"
}

Expected behavior:
The code should not compile, because properties multipleOf and format don't exist in type Float.

Actual behavior:
The code compiles without errors. Typescript version 3.1.6 is the last version which behaves as expected and prints the error message.

Removing the Bool interface from the union, or changing the type parameter on the Bool interface to any other type causes expected behavior.

Not using inheritance and defining the value property in each interface separately does not change the behavior.

Playground Link:
LINK

Related Issues: -

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions