3 ms·
Ah! Thanks for pointing this out, I'll be sure update the post. (I haven't used TypeScript all that extensively, so I didn't even know about `const enum`.)
by z1mm32m4n 8y ago
Ah! Thanks for pointing this out, I'll be sure update the post. (I haven't used TypeScript all that extensively, so I didn't even know about `const enum`.)
- pluma 8y agoFYI here's a variant that eliminates the `impossible` function and instead uses an assignment to the same effect: https://www.typescriptlang.org/play/#src=const%20enum%20ScreenType%20%7B%0D%0A%20%20%20%20LoadingScreen%2C%0D%0A%20%20%20%20CodeEntryScreen%2C%0D%0A%20%20%20%20SuccessScreen%2C%0D%0A%20%20%20%20FailureScreen%2C%0D%0A%7D%0D%0A%0D%0Aconst%20needsCancelButton%20%3D%20(screen%3A%20ScreenType)%3A%20boolean%20%3D%3E%20%7B%0D%0A%20%20switch%20(screen)%20%7B%0D%0A%20%20%20%20case%20ScreenType.LoadingScreen%3A%0D%0A%20%20%20%20%20%20return%20true%3B%0D%0A%20%20%20%20case%20ScreenType.CodeEntryScreen%3A%0D%0A%20%20%20%20%20%20return%20true%3B%0D%0A%20%20%20%20case%20ScreenType.SuccessScreen%3A%0D%0A%20%20%20%20%20%20return%20false%3B%0D%0A%20%20%7D%0D%0A%20%20let%20impossible%3A%20never%20%3D%20screen%3B%0D%0A%20%20return%20impossible%3B%0D%0A%7D%0D%0A https://www.typescriptlang.org/play/#src=const%20enum%20Scre... This is what uglify makes of that: var needsCancelButton = function (e) { switch (e) { case 0: case 1: return!0; case 2: return!1 } return e }; EDIT: Just using `return screen` without the reassignment also works because the return type of the function and the type of the argument are incompatible, but that seems a lot more error prone and less widely applicable that explicitly reassigning to a `never` variable and returning that.