Microsoft/TypeScript

Different `typeof` behavior between ES targets

Open

#49.394 geöffnet am 5. Juni 2022

Auf GitHub ansehen
 (4 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
BugDomain: BinderHelp Wanted

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

Bug Report

🔎 Search Terms

Sorry, hard to formulate.

🕗 Version & Regression Information

  • This bug happens at least since version 3.3.3 (current lowest in playground)
  • This bug happens only when target is greater than es5

⏯ Playground Link

Playground link with relevant code

💻 Code

declare function create<T>(fn: (get: () => T) => T): T

// Error: Cannot find name '_self'.
const store = create((get: () => typeof _self) => {
    const _self = {
        id: 123
    }

    return _self
})

🙁 Actual behavior

If tsconfig property target is greater than es5 TS gives an error: Cannot find name '_self', yet type of store is inferred correctly. For es3 and es5 it works fine.

🙂 Expected behavior

No error 😄


I think it could be related to hoisting rules. Because es5 doesn't have let and const keywords and var is considered to be visible anywhere inside function. But maybe I'm wrong.

Anyway, it would be nice if it worked, as it allows to help the compiler to infer circularly dependent types. Here is the actual example: https://github.com/pmndrs/zustand/discussions/991.

Contributor Guide