You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exhaustiveness checking doesn't work with variables #12668
If you have exhaustiveness checking where a result is a return from a function it works.
But if result is assigning value to a variable exhaustiveness checking basically does nothing.
Code
interfaceSquare{kind: "square";size: number;}interfaceRectangle{kind: "rectangle";width: number;height: number;}interfaceCircle{kind: "circle";radius: number;}interfaceTriangle{kind: "triangle";side: number;}typeShape=Square|Rectangle|Circle|Triangle;functionarea(s: Shape): number{letarea;switch(s.kind){case"square": area=s.size*s.size;break;case"rectangle": area=s.width*s.height;break;case"circle": area=Math.PI*s.radius*s.radius;break;case"triangle": area=Math.sqrt(3)/4*s.side*s.side;break;//default: area = -1; break;}//Error because area is number | undefinedreturnarea;}functionareaWrapped(s: Shape): number{letarea;area=(()=>{switch(s.kind){case"square": returns.size*s.size;case"rectangle": returns.width*s.height;case"circle": returnMath.PI*s.radius*s.radius;case"triangle": returnMath.sqrt(3)/4*s.side*s.side;}})();returnarea;}
Expected behavior:
Function area: variable aria should be number because the check is exhaustive.
Sidenotes:
If I uncomment default then tsc believes that check is exhaustive.
areaWrapped uses IIFE to achieve the same effect and it works.
Actual behavior:
Variable area is of type number | undefined while it can't be undefined.
There are architectural / phase-ordering things that make this more complex than it first appears. Anyway adding a sensible default: throw new Error(); case makes the error go away, or add let! area;
Activity
RyanCavanaugh commentedon Jul 29, 2019
There are architectural / phase-ordering things that make this more complex than it first appears. Anyway adding a sensible
default: throw new Error();case makes the error go away, or addlet! area;