Last active
December 30, 2017 22:58
-
-
Save aliceklipper/9e6bb847deff6597e4f51ab6be60fdd2 to your computer and use it in GitHub Desktop.
Typelevel stuff in Flow
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
// Basic typelevel boolean functions. | |
type If<A: boolean, Then, Else = empty> = $Call<typeof If_, A, Then, Else>; | |
declare function If_<Then, Else>(true, Then, Else): Then; | |
declare function If_<Then, Else>(false, Then, Else): Else; | |
type Not<A: boolean> = If<A, false, true>; | |
type And<A: boolean, B: boolean> = If<A, B, false>; | |
type Or<A: boolean, B: boolean> = If<A, true, B>; | |
type Eq<A: boolean, B: boolean> = If<A, B, Not<B>>; |
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
// Example type predicate. | |
type IsSimple<A> = $Call<typeof isSimple, A>; | |
declare function isSimple(string): true; | |
declare function isSimple(number): true; | |
declare function isSimple(boolean): true; | |
declare function isSimple(null): true; | |
declare function isSimple(void): true; | |
declare function isSimple(empty): true; | |
declare function isSimple(any): false; | |
// Example usage: | |
type A<T> = If<IsSimple<T>, { value: T }, T>; |
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
// Example of pattern matching and recursion. | |
type CollectionType<A> = $Call<typeof collectionType, A>; | |
declare function collectionType<A>(Array<A>): CollectionType<A>; | |
declare function collectionType<A>(Set<A>): CollectionType<A>; | |
declare function collectionType<A>(Map<any, A>): CollectionType<A>; | |
declare function collectionType<A>(A): A; | |
// Example usage: | |
let a: CollectionType<Array<Set<Map<string, number>>>>; // number |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment