I have a state variable that is of type null | SOME_TYPE. But some functions expect it to be SOME_TYPE and those functions are 100% sure that the state variable will be SOME_TYPE when they run.
Can I type cast/assert a variable that will be valid for a whole function scope?
Example:
const [myState,setMyState] = useState<null | SOME_TYPE>(null);
function doSomething() {
// THIS FUNCTION NEEDS myState TO BE SOME_TYPE
// IT'S 100% GUARANTEED THAT WHEN IT RUNS, myState WILL BE SOME_TYPE
// IT WILL ACCESS MULTIPLE PROPERTIES OF MY STATE
console.log(myState.someProp as SOME_TYPE);
console.log(myState.someOther as SOME_TYPE);
console.log(myState.another as SOME_TYPE);
}
Instead of multiple and repetitive type casts, can I do a "block" type assertion (without creating a new variable)?
Example:
function doSomething() {
// myState as SOME_TYPE
// THE IDEA IS THAT INSIDE THIS WHOLE BLOCK myState IS CONSIDERED TO BE SOME_TYPE
console.log(myState.someProp);
console.log(myState.someOther);
console.log(myState.another);
}
Is there a syntax or a proposal for that?