#Higher-order type arguments?
12 messages · Page 1 of 1 (latest)
Preview:ts type Wrapped<T> = {value: T} type WrapTupleValues<T extends any[]> = { [K in keyof T]: Wrapped<T[K]> } type TestTuple = [number, string, "a"] type WrappedTuple = WrapTupleValues<TestTuple> // ^?
E.g.,
type WrapTupleValues<T extends any[], TWrapper> ={
[K in keyof T]: TWrapper<T[K]>
}
Though this as written obviously doesn't work, because TWrapper<T[K]> is not known to take a type argument. It seems like passing around types-as-functions to a generic type parameter isn't possible. Is it?
It is not possible using the usual generic syntax
But there is this library
https://github.com/poteat/hkt-toolbelt
Which uses an approach using interfaces and the this keyword
I can show a minimal example of how it works in a few minutes
Preview:ts ... const x: Result = [ {value: 123}, {value: "abc"}, {value: "a"}, ]
@astral oracle
!:hkt
Here's the HKT encoding I often use:```ts
interface HKT {
input: unknown,
output: unknown,
}
type CallHKT<F extends HKT, I> =
(F & { input: I })["output"]
interface ArrayHKT extends HKT {
output: Array<this["input"]>
}
type X = CallHKT<ArrayHKT, number> // Array<number>
Fascinating!
I like what I see in that library, but my concern is that it may be almost unapproachable for the developers I work with...
Obviously it enables cool things that couldn't otherwise be done, and it's powerful and in many ways simplifies things that would otherwise be very difficult, but I worry that using those things with this library elevates the learning curve too much.