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/bdedab00a25752d2b11a49e72e44a648 to your computer and use it in GitHub Desktop.

Select an option

Save dewey92/bdedab00a25752d2b11a49e72e44a648 to your computer and use it in GitHub Desktop.
Phantom Type - 2
class Temp<A> {
private readonly __unit__: A; // structural diff used here
constructor(degree: number) { }
}
interface Celcius { __celcius__: never } // structural diff
interface Fahrenheit { __fahrenheit__: never } // structural diff
const isBoiling = (temp: Temp<Celcius>) => temp.degree >= 100;
const c100 = new Temp<Celcius>(100);
const f100 = new Temp<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