Is typescript capable of accessing non-common fields of unions without type-guards?

58 Views Asked by At

Given

type union = { a: string, b: number, c: string, d: number } | {a: string, b: number, c: number } | {a: string, b: string }```

I'd like some working syntax similar to (the below does not compile ofc)

  const foo: union = ...;

  foo.a // : string
  foo.b // : number | string
  foo.c // : string | number | undefined
  foo.d // : number | undefined
  foo.e // : either compiler error (or never)

The compiler should already have the info for constructing these anonymous types, the question is whether some syntax exists for this.

0

There are 0 best solutions below