Skip to content

Instantly share code, notes, and snippets.

@RikkiGibson RikkiGibson/adt.ts
Created Mar 11, 2016

Embed
What would you like to do?
Fake ADTs in TypeScript
type Meters = { meters: number };
type Feet = { feet: number };
type Distance = Meters | Feet;
function isMeters(distance: Distance): distance is Meters {
return typeof (distance as Meters).meters === 'number';
}
function isFeet(distance: Distance): distance is Feet {
return typeof (distance as Feet).feet === 'number';
}
function checkDistance(x: Distance): string {
if (isMeters(x)) {
return "meters";
} else if (isFeet(x)) {
return "feet";
}
return "neither";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.