For those who stumble upon this information, there is a great example circulating on the TypeScript discord community:
export interface Hkt<I = unknown, O = unknown> {
[Hkt.isHkt]: never,
[Hkt.input]: I,
[Hkt.output]: O,
}
export declare namespace Hkt {
const isHkt: unique symbol
const input: unique symbol
const output: unique symbol
type Input<T extends Hkt<any, any>> =
T[typeof Hkt.input]
type Output<T extends Hkt<any, any>, I extends Input<T>> =
(T & { [input]: I })[typeof output]
interface Compose<O, A extends Hkt<any, O>, B extends Hkt<any, Input<A>>> extends Hkt<Input<B>, O>{
[output]: Output<A, Output<B, Input<this>>>,
}
interface Constant<T, I = unknown> extends Hkt<I, T> {}
}
There are various use cases for this example code. One key component is defining a SetFactory
, allowing you to specify the desired set type when creating a factory, such as typeof FooSet
or typeof BarSet
. The constructor type takes any T
and returns a FooSet<T>
, similar to a higher kinded type. Methods like createNumberSet
can then be used to create new sets of a specific type, with parameters set accordingly.
interface FooSetHkt extends Hkt<unknown, FooSet<any>> {
[Hkt.output]: FooSet<Hkt.Input<this>>
}
class FooSet<T> extends Set<T> {
foo() {}
static hkt: FooSetHkt;
}
interface BarSetHkt extends Hkt<unknown, BarSet<any>> {
[Hkt.output]: BarSet<Hkt.Input<this>>;
}
class BarSet<T> extends Set<T> {
bar() {}
static hkt: BarSetHkt;
}
class SetFactory<Cons extends {
new <T>(): Hkt.Output<Cons["hkt"], T>;
hkt: Hkt<unknown, Set<any>>;
}> {
constructor(private Ctr: Cons) {}
createNumberSet() { return new this.Ctr<number>(); }
createStringSet() { return new this.Ctr<string>(); }
}
// SetFactory<typeof FooSet>
const fooFactory = new SetFactory(FooSet);
// SetFactory<typeof BarSet>
const barFactory = new SetFactory(BarSet);
// FooSet<number>
fooFactory.createNumberSet();
// FooSet<string>
fooFactory.createStringSet();
// BarSet<number>
barFactory.createNumberSet();
// BarSet<string>
barFactory.createStringSet();
To better understand how this works, let's consider the interaction between FooSet
and number
:
- The crucial point is the type
Hkt.Output<Const["hkt"], T>
. In our case, this translates into Hkt.Output<(typeof FooSet)["hkt"], number>
, which ultimately results in FooSet<number>
.
- We first resolve
(typeof FooSet)["hkt"]
to FooSetHkt
, storing the creation details in the static hkt
property of FooSet
. This step should be repeated for each supported class.
- Next, we have
Hkt.Output<FooSetHkt, number>
. By resolving the Hkt.Output
alias, we get (FooSetHkt & { [Hkt.input]: number })[typeof Hkt.output]
, utilizing unique symbols to ensure uniqueness.
- Now, accessing the
Hkt.output
property of FooSetHkt
, we find the instructions to construct a concrete type with the argument. For FooSetHkt
, the output is defined as FooSet<Hkt.Input<this>>
.
- Finally, through
Hkt.Input<this>
, we access the Hkt.input
property of FooSetHkt
. By tweaking it to include number
, we reach number
as the result, leading to FooSet<number>
.
In essence, the previous discussion surrounding Hkt.Output
applies to the given example but with reversed type parameters:
interface List<T> {}
interface ListHkt extends Hkt<unknown, List<any>> {
[Hkt.output]: List<Hkt.Input<this>>
}
type HigherOrderTypeFn<T, M extends Hkt> = Hkt.Output<M, T>;
// Outputs List<number>
type X = HigherOrderTypeFn<number, ListHkt>;