-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Description
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
Labels
No labels