I'm attempting to develop a function that wraps a value in an array if it is not already an array.
export function asArray<T extends Array<any>>(value: T): T
export function asArray<T>(value: T): T[]
export function asArray(value: any) {
return Array.isArray(value) ? value : [value]
}
However, I've encountered an issue when the value has a union type.
type A = { a: 1 } | { a: 1 }[]
const fn = (v: A) => {
const b = asArray(v)
return b[0].a
}
This results in an error:
Property 'a' does not exist on type 'A'.
Property 'a' does not exist on type '{ a: 1; }[]'.ts(2339)