I have an union of unknown types. I want to get a merge type of it. For example:
const a = {
x: 10
}
const b = {
y: 'qwer'
}
const array = [a, b]
type TArr = typeof array
// I use a function which receives an array as argument so I only have type of array from now on
type TUnion = keyof TArr[number] // { x: number } | { y: string }
type TMerge = ? // { x: number } & { y: string }
Where TMerge
is practically the same as
type TMerge = {
x: number
y: string
}
I can't determine what types are in an array as originally array consists of different classes which used as modules. Without such union merging my final class doesn't know about all the methods and properties.
I assume it's impossible to get merged type right from array because &
's ambiguity between intersection and merge but if there's a trick I would love to hear it.