-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
void | T
allows narrowing of undefined | T
#57855
Comments
Related: #42709 In general a union with |
@MartinJohns Thanks for pointing me to that issue I was tempted to make a feature request for at the very least inferring function returns as undefined by default, if not outright removing void in strict mode. I do agree that in general void shouldn't be used with a union, but it illustrates the inconsistent behaviour of void well. An example without a union is:
|
I think #42709 is the controlling issue here - removing all conflations of |
@RyanCavanaugh I don't agree, that issue seems to be centered around replacing This specific bug will still be present regardless of the decision there. |
There's #46837 then |
Just saw that as well, will close this. |
I still get a kick out of this comment
Given that that was posted over 3 years ago, I think it's safe to say the horse is well and truly dead by now and any further progress on that front will almost assuredly require enlisting the services of a necromancer |
π Search Terms
void, union, undefined, narrowing
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.4.2#code/GYVwdgxgLglg9mABMOdEAoBuAuRm4wAmiAPomCALYBGApgE4CUuFNDpi4htwMYtxAN4AoRIgD04vAWJ0AFgENMfAOaIANjADWtTmG69+hUYhjAMUAJ4AHWnHOZEAXheIARHGoArWtDeNEETExTABuCSl+TAYTMXpaKBB6MFCTAF8TMwxHAEJXLh4+AQCg4PjE5LxwyXIqOnoMahAoRAg4EHVieIV1dUtEOkQFMEsoOVVGdOETcqSU4QzhUEhYBAGFBvRmRABnKHpVQMRZyrc5Wl64N0Q01OE2sD31gC9cLecAPmkiZ3X6O5QcHQ1A2W0Y1UicBaPXUcAA7gIlqhgQpnmCIUNLgjZM1ELR9DtONZjgk5ocFLt9odhEA
π» Code
π Actual behavior
void | number
narrows as if it wereundefined | number
leading to incorrect application state.π Expected behavior
void | number
should only narrow to number usingv === numericLiteral
ortypeof v === "number"
Additional information about the issue
It's a bit unclear whether
typeof v === "object"
should narrow v toobject | null
instead ofnever
, since in some cases like function return types void acts asunknown
.As an aside it seems like
void
isn't very useful now that we haveunknown
(modified playground replacing void with unknown and it catches the issue).The text was updated successfully, but these errors were encountered: