-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Can create variable of type never
from function returning void
by using typeof
#54032
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
Comments
This is like the dual of the situation where someone writes
For the record, |
I don't think this is fixable, except via #42709, without introducing new problems. People (because we haven't cracked the nut on #42709) write const x: string | void = someFuncOrSomeOtherFunc();
if (typeof x === "string") {
// or
if (x !== undefined) {
doSomethingWithString(x);
} Basically for narrowing purposes, Narrowing being fully symmetrical is critical since any narrowing needs to be undoable via unioning of both parts, since this is how CFA works. Otherwise you run into problems where you write const x = someExpr();
x; // <- point a
if (narrowing on x) { /* no-op */ }
x; // <- point b
|
FWIW people really shouldn't be writing |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
1 similar comment
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
#42709 doesn't mention the |
Bug Report
It is possible to create a variable with type
never
from a function returningvoid
, by narrowing it's return value usingtypeof
.This should not be possible.
🔎 Search Terms
never
,void
,typeof
🕗 Version & Regression Information
void
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The code successfully type checks and
f
is called when running even though it has anever
parameter.🙂 Expected behavior
The code should not type check as
v
should be inferred to have typestring
and notnever
inside theif
-body.Alternatively,
typeof v
should not be allowed whenv
has typevoid
.The text was updated successfully, but these errors were encountered: