-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
CommittedThe team has roadmapped this issueThe team has roadmapped this issueFixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
TS 1.4
Type guard fails to narrow the union type when returning early.
function foo(x: number | string) {
if (typeof x === 'string') {
return;
}
// x should now be a number
if (2 % x !== 0) {
// do something
}
}
Unfortunately, I get the following compiler error:
The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
Clearly, the compiler should know at this point that it can't possibly be dealing with a string
, as that scenario has already been returned.
ulrichb, basarat, svieira, mquandalle, bcherny and 2 more
Metadata
Metadata
Assignees
Labels
CommittedThe team has roadmapped this issueThe team has roadmapped this issueFixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript