-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type predicated should eliminate execution path #32648
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
TypeScript does infer that the conditional is always true. const happyFunction = (a: Awesome): number => {
if (isActuallyAwesome(a)) {
return 0
}
if (isNotReallyAwesome(a)) {
return 1
}
const whenAmIReachable: never = a
return a;
} But if you're looking for actual code elimination in the compiled output - that's never going to happen as per:
|
i didn't mean code elimination in the compiled code. I mean that typescript should be able to infer that whatever after the second conditional is dead code. I should not need to write |
Let me give another example: const coolFunction = (): number => {
if (true) {
return 1
}
// typescript won't complain about the return type should be `number | undefined`
} so shouldn't the my example have the same behaviour? |
This issue explains why the literal |
Also covered here: #17358. |
thanks @jack-williams ! |
TypeScript Version: 3.5.1
Search Terms:
Code
Expected behavior:
isNotReallyAwesome
has type signature:(x: Awesome) => x is 'i' | 'am'
. Shouldn't TS be able to tell whenx
is passed in as'i' | 'am'
and therefore infer that it will always return true?I hope this make sense.
Actual behavior:
Playground Link:
https://www.typescriptlang.org/play/index.html#code/C4TwDgpgBAgg7hAzgewLbQLxQOQEttQA+OAhqgcdiQiutgFD0DGyAdosFLojE8AK4kANkJDwkaTFAAUADwBcsGpICUi2V0SlldKBgB8UDRhPaJdRi3aduAOWTAAShGGjxtKXMXvV6zTnwiUnI9Qw0AQlMqHQgGSzYOKAALEjAwEAAxflY+XDY9GRJvGLUoVn5UACMIACdQqABveigWrgAzGW5eAVcxGOkSFRVG5taxmogBGtYoAAZRloBfRjHcDuk7B2den3QBoZGxsYB6Y6gWfiEAEyhQSEQmGtwwTmqoEkqhaGBkLlY22qaexOFwiPrmCD7d5COAkEBaCAAN2EgmASFuv2ANX40BInGASW4UDAyFwrGAAH4FkcJlMZgBGanLakEmrIOABMoQCA3H63JJsjnyaQMZlAA
The text was updated successfully, but these errors were encountered: