-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Generic type that conditionally returns never
can result in a never
acting like any
when used
#50090
Comments
That's working as intended. Nothing is assignable to See also #49862 (comment) and #28982 for more information. |
Yeah, can see that even this works. function alwaysNever(something: string | number): never {
throw Error('err');
}
const ok = alwaysNever('something');
doSomething(ok);
const wtf = alwaysNever(10);
doSomething(wtf);
function doSomething(s: Date) {
s.getDate();
} |
Thanks for the explanation @MartinJohns. I'll close the ticket. It is very unintuitive, but I think I understand why it's happening. |
I think what I'm really wishing for here is something like what is proposed in #23689, a type that is neither assignable to anything nor assignable from anything. |
Yep, #23689 (
Yeah, so tl;dr: If |
Bug Report
🔎 Search Terms
generics, as keyword, type assertion, never
🕗 Version & Regression Information
I see the same behavior in versions 3.3 through 4.8.0 beta in the TS Playground
⏯ Playground Link
Playground link
💻 Code
🙁 Actual behavior
When a generic type that might be
never
is returned from a function by using a type assertion in the return, and the type resolves asnever
, the type is still usable to be passed as an argument to functions, almost as if it was anany
.🙂 Expected behavior
If the compiler is able to resolve a type as
never
, then the variable with that type should cause an error when passed as a function argument.I had this occur on a vastly more complex set of generic types and functions that could sometimes be
never
(and correctly so, as the function did runtime checks and wouldthrow
if passed certain types), and it was very unintuitive that the type could be resolved asnever
but still returned and subsequently passed to another function without an error.The text was updated successfully, but these errors were encountered: