You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect use-before-declaration error in async IIFE initializerΒ #50123
useEffect(()=>{constrequest=(async()=>{try{constresponse=awaitpremiumApi.announcements(client)// @ts-expect-error TypeScript bug, assumes "request" is still in dead zone after awaitif(pendingRequest.current===request||pendingRequest.current===undefined){pendingRequest.current=undefinedsetApiResponse(response)}}catch{pendingRequest.current=undefined}})()pendingRequest.current=requestreturn()=>{pendingRequest.current=undefined}},[client])
π Actual behavior
"Block-scoped variable 'promise' used before its declaration."
"Variable 'promise' is used before being assigned."
π Expected behavior
No errors related to the promise variable.
This is not about the "'await' has no effect on the type of this expression" warning, this is a feature and is expected despite me disagreeing with it.
The first await to be reached in an async function is the point where the function stops being synchronous and yields a Promise for its result. This means that, immediately after the await, the const promise is no longer in TDZ and is well-defined: it is the not-yet-resolved Promise that was produced by the IIFE's invocation. Now, awaiting the promisewould be a deadlock, as the promise would now be waiting for itself to resolve before being able to resolve, but the Promise instance itself is still useful for some purposes like === checks.
I think we can take a simpler fix that is slightly over-pessimistic - the "IIFEs get inlined for CFA" logic shouldn't ever be applied to async functions.
Is it really fixed? I'm still reproducing this issue with any version from 4.8.4 up to 5.4.5 (available in playground), for original code snippet and for destructured snippet from barak igal (@barak007). I'm facing the same issue, and I don't see any resolution.
Is it really fixed? I'm still reproducing this issue with any version from 4.8.4 up to 5.4.5 (available in playground), for original code snippet and for destructured snippet from barak igal (@barak007). I'm facing the same issue, and I don't see any resolution.
I think you're right, thanks for pointing that out. The PR seems to have only fixed the problem when strictNullChecks is off.
Activity
RyanCavanaugh commentedon Aug 2, 2022
I think we can take a simpler fix that is slightly over-pessimistic - the "IIFEs get inlined for CFA" logic shouldn't ever be applied to
asyncfunctions.barak007 commentedon Aug 3, 2022
I have similar case with destructure into
constbindings with default value,Playground:
https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABAcwKZQCoHc4YBYxjIDOAFAJSIDeAUIvYgE7oiNJWIKoBciAjABpEUHL3AATVMEKpxiAL41FNCAmJRqnMKiEi4iALyIKhgHybVYYnAA2qAHQ24yUl105EAegBUiVAA8AB1RoWWF9HEYAa0RvT0p5BUMUdGxcAiIycgBuGhU1DSgM5ABmZJMDc0trO0dnUiLCUpyvT0RIqOJEaW12ggg8RBgu1GJiUZgAQxsbAE9hPFRhDwAjQnEm4nsgA
gabritto commentedon Aug 25, 2022
You might want to file a separate issue about that destructuring-specific case (if an issue for that doesn't already exist).
barak007 commentedon Nov 3, 2022
Gabriela Araujo Britto (@gabritto) is #50199 also fixes my issue?
gabritto commentedon Nov 3, 2022
I don't think so, the fix was only for async functions and in your case your function is not async. Can you open a new issue for your particular case?
artsiommiksiuk commentedon May 23, 2024
Is it really fixed? I'm still reproducing this issue with any version from 4.8.4 up to 5.4.5 (available in playground), for original code snippet and for destructured snippet from barak igal (@barak007). I'm facing the same issue, and I don't see any resolution.
gabritto commentedon May 23, 2024
I think you're right, thanks for pointing that out. The PR seems to have only fixed the problem when
strictNullChecksis off.