Skip to content

Unexpected error "Variable is used before being assigned." #52003

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

Closed
amur-tiger opened this issue Dec 23, 2022 · 4 comments
Closed

Unexpected error "Variable is used before being assigned." #52003

amur-tiger opened this issue Dec 23, 2022 · 4 comments
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@amur-tiger
Copy link

Bug Report

🔎 Search Terms

typeof, void, used before being assigned

🕗 Version & Regression Information

Tested with 4.8.4, 4.9.4, nightly, down to 3.3.3 via Playground

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.9.4#code/MYewdgzgLgBAZmGBeGAKAlALhtATgSzAHMYAfGANxHwBNkA+GAbwFgAoGGAei5xAFsApjGABDADbAAruNFR84dgF8A3O1CRYFCVOEoEGNWw3QYuQRBmwUrDjCgBPAA6DsjlyDiUdepCgBEUmA0gnCEgjT+MAD8MP4AglHY-gBC-gA07Jza4rrYObqZbKrsQA

💻 Code

In this code, fn is a user-supplied function with a generic result (here string) that may also return nothing at all. void is used so return undefined does not have to be written out.

const fn = (): string | void => {
  // some calculation
};
const value = fn();
const result = {
  type: typeof value === "undefined" ? "A" : "B",
  value: value,
};

🙁 Actual behavior

In line 7: TS2454: Variable 'value' is used before being assigned.

Interestingly, the error does not occur when line 6, the typeof check, is commented out.
The error can also be avoided by writing value === undefined instead or casting value to string | undefined.

The error is unexpected because the variable was clearly defined in line 4.

🙂 Expected behavior

I would expect no error, or an error trying to use a void variable. Having to cast it to undefined would be fine.

@fatcerberus
Copy link

fatcerberus commented Dec 23, 2022

You shouldn’t use void in a union with other types; it’s essentially a singularity and causes weird things to happen. Write string | undefined if that’s what you mean. See #42709.

To clarify: As far as the type system is concerned void doesn’t really mean undefined. It means “I don’t know what this value is and it doesn’t matter because nobody should ever access it.”

@amur-tiger
Copy link
Author

👍 As I thought, void is a bit special. Though as I wrote, it's handy for the callback supplier, so I'll keep it and add a cast or something. I just found the error message rather interesting.

@fatcerberus
Copy link

fatcerberus commented Dec 23, 2022

Keep in mind that the check for ret === undefined is technically unsound because () => T is assignable to () => void for all T. This is why string | void is suspect.

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Jan 4, 2023
@github-actions
Copy link

github-actions bot commented Jun 8, 2023

This issue has been marked as 'Not a Defect' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants