3 ms·
Not possible, but an approximation: interface Mappable<P extends Mappable<P, unknown>, T> { flatMap<U>(f: (x: T) => Mappable<P, U>): Mappable<P, U>; }
by karmakaze 6y ago
Not possible, but an approximation:
interface Mappable<P extends Mappable<P, unknown>, T> {
flatMap<U>(f: (x: T) => Mappable<P, U>): Mappable<P, U>;
}
class Maybe<T> implements Mappable<Maybe<unknown>, T> {
x: T | undefined;
public flatMap<U>(f: (x: T) => Maybe<U>): Maybe<U> {
if (this.x) {
return f(this.x);
}
return Maybe.nothing();
}
}
- zcesur 6y agoYes, in fact this reminds me of the HKT implementation[1] found in fp-ts[2][3] interface HKT<F, A> { _URI: F _A: A } interface Mappable<F> { map<A, B>(f: (a: A) => B, fa: HKT<F, A>): HKT<F, B> } where F is a unique identifier representing the type constructor and A its type parameter. [1]: https://www.cl.cam.ac.uk/~jdy22/papers/lightweight-higher-kinded-polymorphism.pdf https://www.cl.cam.ac.uk/~jdy22/papers/lightweight-higher-ki... [2]: https://github.com/gcanti/fp-ts https://github.com/gcanti/fp-ts [3]: https://gist.github.com/gcanti/2b455c5008c2e1674ab3e8d5790cdad5 https://gist.github.com/gcanti/2b455c5008c2e1674ab3e8d5790cd...
- girvo 6y agogcanti’s work in Flow and Typescript is amazing and I use it daily :)