#Can't access property from type which is intersection of object types and (union of objects).

9 messages · Page 1 of 1 (latest)

cedar sigil
#
type Ob = { a: string } & ({ b: string } | { c : string })

type BC = Ob['b'] // cannot do this

Is it possible to access b and c?

odd oasis
#

that's correct, you can only access definite keys. b doesn't exist on { a: string } & { c: string }

cedar sigil
#

Can't access property from type which is intersection of object types and (union of objects).

cedar sigil
full ibex
#

You can distribute it.

fluid umbraBOT
#
type DistributiveIndex<T, K extends PropertyKey> = T extends Record<K, infer V> ? V : never

type Ob = { a: string } & ({ b: string } | { c : string })

type Obb = DistributiveIndex<Ob, 'b'>
//   ^? - type Obb = string
full ibex
#

But it's not common to need something like this.

cedar sigil
cedar sigil