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

Missed "use before declaration" errors #60116

Closed
davidnormo opened this issue Oct 2, 2024 · 2 comments
Closed

Missed "use before declaration" errors #60116

davidnormo opened this issue Oct 2, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@davidnormo
Copy link

davidnormo commented Oct 2, 2024

🔎 Search Terms

"reference error", "Block-scoped variable used before its declaration", "ts(2448)", "Variable is used before being assigned", "ts(2454)"

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries but couldn't find a related issue.

⏯ Playground Link

https://www.typescriptlang.org/play/?jsx=0#code/MYewdgzgLgBAZiEMC8MA8AVAfACjmALhhwEoUsYMSiNz4xSBuAKGYRB1LoE8SXnQkWNyLQATgEswAcxTEuyCryZA

💻 Code

const foo = <T>(fn: () => T): T => fn();

foo(() => y); // no error for reference to y here, but there will be a runtime ReferenceError

const y: string = (() => y)(); // ts will error on the reference to y here inside IIFE

🙁 Actual behavior

TS isn't catching references to variables (let, const or var) before they are defined when used as a parameter to another function call.

🙂 Expected behavior

In the example above, I'd expect it to error on the use of y in the function argument to foo.

Additional information about the issue

I discovered the issue while writing a React component with useMemo and did a silly like this:

const MyComponent = () => {
  const value: string = useMemo(() => value, []); // no ts error but there is a ReferenceError thrown at runtime
  
  return null;
}
@MartinJohns
Copy link
Contributor

Duplicate of #59723 / #11498.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 2, 2024
@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 Oct 5, 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