Last active
September 17, 2018 11:21
-
-
Save dewey92/8fb7c8f21ed479395d62c5664129583c to your computer and use it in GitHub Desktop.
Alternative Phantom Types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Temp<A> { | |
| private readonly __unit__: A; | |
| constructor(degree: number) { } | |
| } | |
| enum Unit { Celcius, Fahrenheit } | |
| const isBoiling = (temp: Temp<Unit.Celcius>) => temp.degree >= 100; | |
| const c100 = new Temp<Unit.Celcius>(100); | |
| const f100 = new Temp<Unit.Fahrenheit>(100); | |
| isBoiling(c100); // type-check! | |
| isBoiling(f100); // error! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment