5 ms·
The TypeScript comment is wrong, the author needs to use a const enum: https://www.typescriptlang.org/docs/handbook/enums.html#const-enums https://www.typescrip
by esprehn 9y ago
The TypeScript comment is wrong, the author needs to use a const enum:
https://www.typescriptlang.org/docs/handbook/enums.html#const-enums https://www.typescriptlang.org/docs/handbook/enums.html#cons...
Then it generates code that's better than Flow. It's not as aggressive as Reason for sure, but it's still pretty good.
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%20impossible%20%3D%20(x%3A%20never)%3A%20never%20%3D%3E%20%7B%0D%0A%20%20throw%20new%20Error('This%20case%20is%20impossible.')%3B%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%20return%20impossible(screen)%3B%0D%0A%7D%0D%0A https://www.typescriptlang.org/play/#src=const%20enum%20Scre...
- huy-nguyen 9y agoI hope the author adds this correction to his article because there’s no way to comment on it directly.
- z1mm32m4n 9y agoAh! 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 9y 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.
- deleted 9y ago[deleted]
- pluma 9y agoNot just that, the initial Flow example can actually be translated 1-to-1 to TypeScript (without using enums at all): https://www.typescriptlang.org/play/index.html#src=type%20AppScreen%20%3D%0D%0A%20%20%7C%20'LoadingScreen'%0D%0A%20%20%7C%20'CodeEntryScreen'%0D%0A%20%20%7C%20'SuccessScreen'%0D%0A%20%20%7C%20'FailureScreen'%3B%0D%0A%0D%0Aconst%20impossible%20%3D%20(value%3A%20never)%3A%20never%20%3D%3E%20%7B%0D%0A%20%20throw%20new%20Error('This%20case%20is%20impossible.')%3B%0D%0A%7D%0D%0A%0D%0Aconst%20needsCancelButton%20%3D%20(screen%3A%20AppScreen)%3A%20boolean%20%3D%3E%20%7B%0D%0A%20%20switch%20(screen)%20%7B%0D%0A%20%20%20%20case%20'LoadingScreen'%3A%0D%0A%20%20%20%20%20%20return%20true%3B%0D%0A%20%20%20%20case%20'CodeEntryScreen'%3A%0D%0A%20%20%20%20%20%20return%20true%3B%0D%0A%20%20%20%20case%20'SuccessScreen'%3A%0D%0A%20%20%20%20%20%20return%20false%3B%0D%0A%20%20%20%20default%3A%0D%0A%20%20%20%20%20%20%2F%2F%20TypeScript%20error%3A%0D%0A%20%20%20%20%20%20%2F%2F%20Argument%20of%20type%20'FailureScreen'%20is%20not%20assignable%20to%20parameter%20of%20type%20'never'.%0D%0A%20%20%20%20%20%20return%20impossible(screen)%3B%0D%0A%20%20%7D%0D%0A%7D https://www.typescriptlang.org/play/index.html#src=type%20Ap...