-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixedFixedA PR has been merged for this issueA PR has been merged for this issue
Description
TypeScript Version: 3.4.0-dev.20190220
Search Terms:
switch case exhaustive assignment assign undefined TS2322
Code
Compile with the flags --noImplicitReturns --noImplicitAny --strictNullChecks
enum Test {
A,
B,
C
}
// this example correctly understands that all possible values of t are handled
function foo(t: Test): number {
switch (t) {
case Test.A:
return 1;
case Test.B:
return 2;
case Test.C:
return 3;
}
}
// this should be identical to foo, since a is always assigned as the switch is exhaustive
function bar(t: Test): number {
let a;
switch (t) {
case Test.A:
a = 1;
case Test.B:
a = 2;
case Test.C:
a = 3;
}
return a; // I don't expect an error here
}
Expected behavior:
Code compiles with no errors. When a
is returned in bar
, its type is number
. Typescript understands that an exhaustive switch with each case assigning to a type-inferred variable means that the variable's type is the union of each assigned value's type. I'd expect this to work because returns within an exhaustive switch are understood in a similar manner.
Actual behavior:
When a
is returned in bar
, its type is number | undefined
TS2322: Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
Playground Link:
link here
Turn on noImplicitReturns
, noImplicitAny
, and strictNullChecks
.
Related Issues:
- Initializing
let
variables and exhaustiveness checking #22470 (different, since the default doesn't assign) - TS2454 after assigning a value from an exhaustive switch statement #18362 (this is pretty old, from TS 2.4.2. Since
foo
in my example works I think the closure reason might be invalid)
SlurpTheozpdDG4gta8XKpMCd
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixedFixedA PR has been merged for this issueA PR has been merged for this issue