I have a piece of code that triggers the title issue but not the other, they do the same thing and have no clue why.
the one who triggers the TS2345 (I tried to type and not typing data here, same result)
const data : Element[]|null = usePewFetcher(
projectId ?
pewFetcher.projectApplicationsFromProject({id: projectId}):
pewFetcher.applications({})
, []
)
the one okay
let data;
if( projectId ) {
data = usePewFetcher(
pewFetcher.projectApplicationsFromProject({id: projectId}) //return type : ApplicationProject[] (extending Element)
, []
)
} else {
data = usePewFetcher(
pewFetcher.applications({}) //return type : Application[] (extending Element)
, []
)
}
export function usePewFetcher<T>(pewFetcher: Promise<T>, deps?: any[]): T|null {
const [data, setData] = useState(null)
useEffect(() => {
const call = async () => {
setData(await pewFetcher as any)
}
call()
}, deps)
return data;
}
Application and ApplicationProject are both inherited from Element but they have different properties