Skip to content

Instantly share code, notes, and snippets.

@justanotherdot
Created September 3, 2017 22:18
Show Gist options
  • Save justanotherdot/ce3d94886826c39c67acf21ef62c91e3 to your computer and use it in GitHub Desktop.
Save justanotherdot/ce3d94886826c39c67acf21ef62c91e3 to your computer and use it in GitHub Desktop.
interface Some<V> {
tag: 'Some';
value: V;
}
// A nullary type, which is just a tag at runtime for our purposes.
interface None {
tag: 'None';
}
type Option<V> = Some<V> | None;
const safeHead = (xs: number[]): Option<number> => {
if (xs instanceof Array) {
if (xs.length === 0) {
return {
tag: 'None'
};
} else {
return {
tag: 'Some',
value: xs[0],
}
}
} else {
const _ex_patt_match: never = xs;
}
};
const xs = [1,2,3];
const mb = safeHead(xs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment