I want to do this
const tuple = ["foo", "bar"] as const
const convert = <T>(x: T): {item: T} => ({item: x})
const convertedTuple: [{item: "foo"}, {item: "bar"}] = tupleMap(tuple, convert)
I have tried this
type TupleMap<T extends readonly any[], Fn extends (item: any) => any>
= T extends readonly [infer Head, ...infer Tail]
? (Fn extends (item: Head) => infer R
? [R, ...TupleMap<Tail, Fn>]
: never)
: []
function tupleMap<T extends readonly any[],
Fn extends (item: any) => any>(a: T, fn: Fn): TupleMap<T, Fn> {
return a.map(fn) as any
}
But convertedTuple
type is inferenced as [{item: unknown}, {item: unknown}]
and I am not sure why.