Higher-kinded types for Flow
Extending the HigherType
class to define new higher-kinded types.
A list of types. Head
is the first type in the list, and Tail
is either $End
or another $List
.
import { type $List } from 'flow-higher';
The end of a $List
. The only value of type $End
is End
.
import { type $End, End } from 'flow-higher';
The head type of the given $List
.
import type { $List, $End, $Head } from 'flow-higher';
type TList = $List<string, $List<number, $End>>
// extract the head (ie the first element)
('foo': $Head<TList>); // OK
The tail of the given $List
. Either another $List
or $End
.
import type { $List, $End, $Head, $Tail } from 'flow-higher';
import { End } from 'flow-higher';
type TList = $List<string, $List<number, $End>>
// extract the tail list
([ 42, End ]: $Tail<TList>); // OK
// extract the head of the tail list (ie the 2nd element)
(42: $Head<$Tail<TList>>); // OK
Convenience types for $List
s of up to six types.
import type { $1list, $2List, $3list } from 'flow-higher';
Defines a new higher-kinded type.
import { type $2Types, Type } from 'flow-higher';
class IsPair {}
export class Pair<A, B> extends Type<IsPair, $2Types<A, B>, [A, B]> {
static Both<A, B>(a: A, b: B): Pair<A, B> {
return Pair._wrap(IsPair, [a, b]);
}
}
Takes a class as the kind and a compatible value and returns a HigherType
instance.
Takes a class as the kind and a compatible HigherType
instance and returns the contained value.