#type assertion

5 messages · Page 1 of 1 (latest)

simple vessel
#

i have a union type t3 where only one of the fields can be defined

type t1 = {
  a: int;
  b: undefined;
}

type t2 = {
  a: undefined;
  b: int;
}

type t3 = t1 | t2;

v1 and v2 are int | undefined but i know that only one of the values will be set. how can i assert that a is t3?

const { v1, v2 } = values

const a: t3 = {
  a: v1,
  b: v2,
}
full oxide
#

once you destructure, v1 and v2 no longer have the "only one is set" linkage as t3 does.

#

same for once you property access actually

#

you could spread values in if the keys were the same

simple vessel
#

keys aren't the same so is type assertion the only way? are type assertions an escape hatch from TS