Created
September 3, 2017 22:18
-
-
Save justanotherdot/ce3d94886826c39c67acf21ef62c91e3 to your computer and use it in GitHub Desktop.
This file contains 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
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