#satisfies at object level and property level

6 messages · Page 1 of 1 (latest)

marble cobalt
#

why do i need to use satisfies at the first example and not in the second one

type exampleA = {
var: string | number;
}

const example1: exampleA = {
var: 10
}

example1.var.toFixed(2); // Error....

type exampleB = {var: number} | {var: string};

const example2: exampleB = {
var: 10
};

example2.var.toFixed(2);

fair raven
#

example2 can be narrowed to { var: number } because ts sees that it's using that branch

#

that's an exception, not the rule

marble cobalt
fair raven
#

in the former, the union is only on var, ts would have to narrow example1.var rather than the entire example1

#

you can mutate example1 from string to number or vice versa