Is there a way to apply TypeScript assuming a variable's type after a function checking?
For example:
const variable: string | undefined = someFunction()
variable && setAppTitle(variable) // getting sure that a variable is a defined string
But in my app I have the following code:
const [data, setData] = useState<ResponseData>() // data variable is of type ResponseData | undefined
const [isLoading, setIsLoading] = useState<boolean>(true)
useEffect(() => {
// load data on mount
onInit()
async function onInit() {
const response = await fetch("https://example.com")
const data = await response.json()
setData(data)
setIsLoading(false)
}
}, [])
const isLoaded = (): boolean => {
// some complex logic to check if the data is loaded and correct
return !isLoading && !!data && Object.keys(data).length > 0
}
return (
<>
<div>My data title:</div>
{isLoaded() && <div>{data.title}</div>} // error! data could be "undefined"
</>
)
So there is entire function checking a bunch of stuff.
Is there a way to point out to TypeScript that I have checked if the data variable is not undefined other than using data!.title?
I can't afford putting the whole checking line into the JSX as this would blur the code and there is too much stuff going on. Or maybe there is a better way around this?