#Can't access property from type which is intersection of object types and (union of objects).
9 messages · Page 1 of 1 (latest)
that's correct, you can only access definite keys. b doesn't exist on { a: string } & { c: string }
Can't access property from type which is intersection of object types and (union of objects).
cannot access c as well only a. Is there any workaround?
You can distribute it.
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
But it's not common to need something like this.
Thanks @full ibex it worked.
yeah, I used it here.