You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
constfn=(): string|void=>{// some calculation};constvalue=fn();constresult={type: typeofvalue==="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.
The text was updated successfully, but these errors were encountered:
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.”
👍 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.
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.
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 soreturn undefined
does not have to be written out.🙁 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 castingvalue
tostring | 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.The text was updated successfully, but these errors were encountered: