-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Never type is not inferred when used from helper function #33193
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
Function should accept param of type function getSomethingHelperNeverNoFail(foo: UnionType): number {
switch (foo) {
case "A":
return 1;
default:
return never(foo); // you should pass param to function to enable type check
}
}
function never(val: never): never {
throw new Error("Should never happen");
} UPD. You can even provide helpful exception: function never(val: never): never {
throw new Error("Nothing expected, but got: " + val); // or JSON.stringify(val)
} |
"Function should accept param of type never, otherwise there should be no checks", but why? Shouldn't the never type be inferred from the return value of the function? |
|
There are no problem with returning As for handling functions with And you can see related discussion in comment: #14490 (comment) |
Thanks for the links and explanation. Should I close this issue, since there is overlap with linked issues, or is there some value to keeping it open? |
Not sure if there are any actionable items left, so most likely this issue should be closed. However if you are sure that there are some you can let if open for TS team to decide. |
TypeScript Version: 3.5.1
Code
Expected behavior:
There is type error in getSomethingHelperNeverNoFail function
Actual behavior:
No type error in getSomethingHelperNeverNoFail function
Playground Link: http://www.typescriptlang.org/play/#code/FAFwngDgpgBAqgOwJYHsEBVKwLwwEQCCeMAPvgEJ7DABmArggMYioIwDmUIAyigLZcAFkgTsAFDRQoAXPGRpM0AJSyEdPgCMoAJxgBvYDCMwAzgHckIRoJgSpS-YePPGAQxOxCeaU+d+Y2lx02mwAjADcvn5uHhTeUf5GgSDBbABMCcYAJlA0rnQANiA+idFoJiAwAPqqUABuOjC4kiiRpc7JqdVtxgC+wP20DMysHFy8AiDCogCSCAUiUABy9ToAYq5IBXYycqyKUCowapqNBs7mlta2LQ7niTGeRCXtSUEhMBGZRjl5hcXfMoICrVWoNXTNKThGAAehhMAOMAA5HhKEiYEgTMcUJV3CYkOwEK4NAVYCAUDBwNBkQhVtokQA6QEdd5sKo9Iz9Qb0JgsNBjHj8IQidgACSgBWg2hW4KWKA2Wx2skQ+ywRxOWl09yMlysNh2d0Bj3wz2Zxk6Hy+pV++SKL1eFrYtPBYiU0LhMDllKwMB02hQ2iiXOoPJG-OdOldYLOUSm-rMxygCYAotp-doxHhuIIUIUsonwTBBK4INAEHg3QMgA
The text was updated successfully, but these errors were encountered: