'none of those signatures are compatible with each other' error when calling find() on a value with two possible array types
Solution 1:
Your problem is that the type of desc
is ABC[] | DEF[]
.
That means you need to supply desc.find
with a function that can iterate over the elements of either a ABC[]
or a DEF[]
. That's what the error message is telling you.
Assuming the intent of your code is to ONLY look for an ABC
in desc
, the following code will do so safely, first checking to make sure desc is an ABC[]
instead of a DEF[]
. You can also test it in TS Playground.
type ABC = {
title: string
}
type DEF = {
name: string
}
type XYZ = {
desc: ABC[] | DEF[]
}
const container: XYZ = {
desc: [{ title: 'abc' }, { title: 'def' }]
}
const { desc } = container
let result: ABC | undefined
// only do the search if desc is an ABC[]
if (desc[0] && 'title' in desc[0]) {
// Typescript isn't smart enough to figure out that
// desc is now guaranteed to be an ABC[], we need
// to include a type assertion
result = (desc as ABC[]).find((t: ABC) => t.title === 'abc')
}
console.log(result)