Is there a method to safely map over the union of arrays without hard-coding specific types?
When attempting to calculate newArr1
, an error is encountered:
Property 'field2' does not exist on type 'Common<A, B>'
. How can this error be replicated for newArr2
as well?
(link to code)
interface A {
field1: string
field2: number
}
interface B {
field1: string
}
type Common<A, B> = {
[P in keyof A & keyof B]: A[P] | B[P];
}
function mapArray(arr: A[] | B[]) {
const newArr1 = (arr as Common<A, B>[]).map(i => i.field2) // error
// how can I get the same error without hard-coding the types from the type union?
const newArr2 = (arr as MagicEventuallyFromJcalz<typeof arr>).map(i => i.field2)
return newArr1
}