Skip to content

Intersections of types that do not have intersection do not collapse to never correctly #33517

@SCLeoX

Description

@SCLeoX

TypeScript Version: 3.6.2

Search Terms:
TypeScript intersection collapse never auto narrowing auto collapse
Code

export type RuntimeValue = {
  type: 'number',
  value: number,
} | {
  type: 'boolean',
  value: boolean,
} | {
  type: 'string',
  value: string,
};

const a: RuntimeValue & { type: 'number' } = blackBox();

Math.abs(a.value);

Expected behavior:
No error, since the only intersection between { type: 'number' } and RuntimeValue is { type: 'number', value: number }.

Actual behavior:
Error thrown on Math.abs.
Argument of type 'string | number | boolean' is not assignable to parameter of type 'number'. Type 'string' is not assignable to type 'number'.ts(2345)

Playground Link:

Related Issues:

P.S.

The type of a is:

({
    type: "number";
    value: number;
} & {
    type: "number";
}) | ({
    type: "boolean";
    value: boolean;
} & {
    type: "number";
}) | ({
    type: "string";
    value: string;
} & {
    type: "number";
})

which should collapse to

{
    type: "number";
    value: number;
} | never | never

which should be equivalent of

{
    type: "number";
    value: number;
}

Thus a.value should be number

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