Skip to content

No typechecking error in code that uses an uninitialized variable due to circular reference #59723

@jthemphill

Description

@jthemphill

🔎 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

// 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 stuff
function fetchCachedValues<V>(fetchOnCacheMiss: () => V): V {
  return fetchOnCacheMiss();
}

const getCoolnessUsingCache = (): boolean => {
  const isCool = fetchCachedValues((): boolean => {
    // This typechecks just fine, even though `isCool` is not initialized here!
    return isCool;
  });
  return isCool;
}

// 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions