Trying to find a way to indicate the expected return type of Array.find()
in TypeScript, I encountered an incompatibility warning.
class A {
"type"="A"
t: string;
#a = 0
constructor(t: string) {
this.t = t;
}
}
class B {
"type"="B"
t: string;
constructor(t: string) {
this.t = t;
}
}
type All = A | B
function trouve(arr:All[]):A {
return arr.find(x => x.type === "A");
}
TypeScript highlights the return
line with the following message:
Type 'All | undefined' is not assignable to type 'A | undefined'
While I recognize that the parser may struggle to understand it, my intention is to eliminate this warning.