This code works:
class A {}
class B {}
class C {}
const classCFromAOrB = (element: A | B): C => new C()
const a: A[] | B[] = [new A()]
const c: C[] = a.map(element => classCFromAOrB(element))
This code doesn't:
import { classA } from '../some'
import { classB } from './../some'
interface interfaceC {}
const render = (element: classA | classB): interfaceC => {
return {}
}
interface ResultsScreenProps {
resultsScreenPresented: boolean
answers: classA[] | classB[]
dismiss: SimpleFunc
}
const Screen: React.SFC<ResultsScreenProps> = (props) => {
const array: classA[] | classB[] = props.answers
const second: interfaceC[] = array.map(el => render(el)) // here is the error
...
}
On the line defining second
I'm getting an error:
[ts] Cannot invoke an expression whose type lacks a call signature. Type '((callbackfn: (value: classA, index: number, array: classA[]) =>...' has no compatible call signatures.
What am I doing wrong?
The error is reproducible if classA looks like this:
class classA {
anyArg: number
constructor(anyArg: number) {
this.anyArg = anyArg
}
}
ResultsScreenProps
? – Explosion PillsSimpleFunc
? – Explosion PillsclassA[] | classB[]
to(classA | classB)[]
, or widen the value to that type before you callmap
on it. – jcalz