Why does TypeScript throw an error when testing the presence of a promise in an object?
This condition will always return true since this 'Promise' is always defined.
Did you forget to use 'await'?
Should I make changes to my types?
const xRecord: Record<string, Promise<number>> = {}
const generateX = async () => {
// long async stuff here
return Math.random()
}
const getX = async (key: string) => {
if(xRecord[key]){ // TypeScript error occurs here
return xRecord[key]
}
return xRecord[key] = generateX()
}
const x = await getX('foo')
const x2 = await getX('foo')