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
// In the real code, all of these functions are async and return promises;// I just made everything synchronous here to minimize the test case// The real version of this function does caching stufffunctionfetchCachedValues<V>(fetchOnCacheMiss: ()=>V): V{returnfetchOnCacheMiss();}constgetCoolnessUsingCache=(): boolean=>{constisCool=fetchCachedValues((): boolean=>{// This typechecks just fine, even though `isCool` is not initialized here!returnisCool;});returnisCool;}// Runtime error: Cannot access 'isCool' before initialization getCoolnessUsingCache();
🙁 Actual behavior
TypeScript reports no errors. Running the code gives a runtime error: Cannot access 'isCool' before initialization
🙂 Expected behavior
I think TypeScript should report an error! Possibly Block-scoped variable 'isCool' used before its declaration.(2448)
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
If fetchCachedValues defers execution of its callback, this code is fine, but we don't know if it defers or not. If we did error, there'd be no 'correct' way to write correct code, thus there isn't an error.
Yeah, after reading #11498 I understand why that would resolve this issue.
I'm mostly surprised, and annoyed, that ECMAScript allows
constx=function(){returnx;}
in the first place, and I agree that a defer keyword in TypeScript would let the typechecker differentiate between lambdas that do and don't work with circular references.
🔎 Search Terms
uninitialized variable, lexical scoping, circular reference
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&allowUnreachableCode=true&allowUnusedLabels=true&noUnusedLocals=true&noUnusedParameters=true&target=99&strictBuiltinIteratorReturn=true&useUnknownInCatchVariables=true&exactOptionalPropertyTypes=true&noFallthroughCasesInSwitch=true&noImplicitOverride=true&noPropertyAccessFromIndexSignature=true&suppressImplicitAnyIndexErrors=true&ts=5.7.0-dev.20240822#code/PTAEEkDtQFwCwKagE4IIYBtQGMD2ATBAGlEy1wDNZEBnJCgV0mxgEtdIbTVSaBPZqUj4UCGA2TQADslwBbVnRoBuAFAgIoAFYMaMUHLSFQCAG4JkfeK0gBzUP2ZxZkXLtCIeMXAZusFAF5I8MEIejhodKrqYAAqiKKYoObINOzQlNSKoIzMbByg+LhhEdhwNvZ6DBQUqrks6TliZQDCaGUI+ABqmAxhADxdAHwAFBTNcADykG0dALKKNABcoCMAlKAAvEOgXWsrXaAA3qqgouKSTTBl07OICzQ062oAvtF4nPq2Yi24uBiQMI0ACqaTsdyQm1W+1AACM-hh0NBtsdTjgOOFFL9-lsrq12ohur0wiN1it4f8kVsdiczmcNPFsjA+FIEB1sABrLg6cIUGzEEzmaDwNy2OCgAAGWIREtA2Vc+j8bEwrCCIk8CAAhGizqgLtBpf81GcXmtjecJAaaNiMK9ohoAEpMNhyJAWWTIFZtSAK0jYbBA0AAckNGCDcIQFFwPCVrBVATQ+Wgqm+MBtgMeoIqEOeqiAA
💻 Code
🙁 Actual behavior
TypeScript reports no errors. Running the code gives a runtime error:
Cannot access 'isCool' before initialization
🙂 Expected behavior
I think TypeScript should report an error! Possibly
Block-scoped variable 'isCool' used before its declaration.(2448)
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: