Always assigning to an un-initialized variable in an exhaustive switch #30000
Labels
Design Limitation
Constraints of the existing architecture prevent this from being fixed
Fixed
A PR has been merged for this issue
TypeScript Version: 3.4.0-dev.20190220
Search Terms:
switch case exhaustive assignment assign undefined TS2322
Code
Compile with the flags
--noImplicitReturns --noImplicitAny --strictNullChecks
Expected behavior:
Code compiles with no errors. When
a
is returned inbar
, its type isnumber
. 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 inbar
, its type isnumber | undefined
Playground Link:
link here
Turn on
noImplicitReturns
,noImplicitAny
, andstrictNullChecks
.Related Issues:
let
variables and exhaustiveness checking #22470 (different, since the default doesn't assign)foo
in my example works I think the closure reason might be invalid)The text was updated successfully, but these errors were encountered: