#Higher-order type arguments?

12 messages · Page 1 of 1 (latest)

astral oracle
#

Given the following code, is there some way to change type WrapTupleValues to accept a second generic type argument that specifies the type helper to use to do the wrapping?

hollow hareBOT
#
emtucifor#0

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> // ^?

astral oracle
#

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?

earnest roost
#

I can show a minimal example of how it works in a few minutes

hollow hareBOT
#
mishall8399#0

Preview:ts ... const x: Result = [ {value: 123}, {value: "abc"}, {value: "a"}, ]

earnest roost
#

@astral oracle

golden kayak
#

!:hkt

hollow hareBOT
#
tjjfvi#0
`!t6: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>

astral oracle
#

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.