Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jthemphill opened this issue Aug 22, 2024 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@jthemphill
Copy link

jthemphill commented Aug 22, 2024

🔎 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

@MartinJohns
Copy link
Contributor

Essentially a duplicate of #11498.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 26, 2024
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Aug 26, 2024

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.

@jthemphill
Copy link
Author

Yeah, after reading #11498 I understand why that would resolve this issue.

I'm mostly surprised, and annoyed, that ECMAScript allows

const x = function() { return x; }

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.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants