Below is a minimal representation of my problem. I have a type that's a union of ReadonlyArrays. Normally when I want a shallow copy I can do something like Array.from(array) or [...array] to get a shallow copy. It doesn't work for these unions, even though logically it should be sound.
Is there an easy/ergonomic way to get the type system happy w/o sacrificing performance?
function shallowCopy1(value: ReadonlyArray<string> | ReadonlyArray<number>): string[] | number[] {
return [...value]
}
function shallowCopy2(value: ReadonlyArray<string> | ReadonlyArray<number>): string[] | number[] {
return Array.from(value)
}
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.