Skip to content

Instantly share code, notes, and snippets.

@dewey92
Last active September 17, 2018 11:21
Show Gist options
  • Select an option

  • Save dewey92/8fb7c8f21ed479395d62c5664129583c to your computer and use it in GitHub Desktop.

Select an option

Save dewey92/8fb7c8f21ed479395d62c5664129583c to your computer and use it in GitHub Desktop.
Alternative Phantom Types
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