Although my solution is not an exact match for your needs, it could serve as a temporary fix.
Keep in mind that this solution is specific and works only with the foo
type signature.
function foo<T>(bar: T, baz: number) { }
type Fn = (...args: any) => any
type Tail<T extends any[]> = T extends [infer _, ...infer Rest] ? Rest : never
type ReplaceFirstParameter<F extends Fn, Fst> =
(fst: Fst, ...args: Tail<Parameters<F>>) => ReturnType<F>
type Foo<T> = ReplaceFirstParameter<typeof foo, T>
type Result = ReplaceFirstParameter<typeof foo, number> // (fst: number, baz: number) => void
This updated solution offers more generality and will replace all unknown
parameters with the provided generic argument.
Please note that I make the assumption that every function's generic is unknown - which may be a weak point
type MapPredicate<T, V> = T extends unknown ? V : T
type ReplaceUnknown<
Arr extends Array<unknown>,
Value,
Result extends Array<unknown> = []
> = Arr extends []
? []
: Arr extends [infer H]
? [...Result, MapPredicate<H, Value>]
: Arr extends [infer Head, ...infer Tail]
? ReplaceUnknown<[...Tail], Value, [...Result, MapPredicate<Head, Value>]>
: Readonly<Result>;
function foo<T>(bar: T, baz: number) { }
type Fn = (...args: any) => any
type ReplaceParameters<F extends Fn, Value> =
(...args: ReplaceUnknown<Parameters<F>, Value>) => ReturnType<F>
type Result = ReplaceParameters<typeof foo, number> // (fst: number, baz: number) => void
Playground