小编yah*_*wai的帖子

替换函数打字稿中最后一个参数的类型

我想替换函数中最后一个参数的类型,同时保留所有函数参数的名称。就我而言,最后一个参数也是可选的。

例如:

type OrigArg = { arg: number };
type ReplacedArg = { arg: string };

type OrigFunc = (a: number, b: string, c?: OrigArg) => string
type ReplaceLast<TFunc, TReplace> = // type I'm looking for

type ReplacedFunc = ReplaceLast<OrigFunc, ReplacedArg> 
// type ReplacedFunc = (a: number, b: string, c?: ReplacedArg) => string
Run Code Online (Sandbox Code Playgroud)

更复杂的是前面的参数的数量和类型是可变的,我只知道最后一个参数将是某种类型,我想用自定义的类型替换它。

parameters tuples typescript

4
推荐指数
1
解决办法
1990
查看次数

标签 统计

parameters ×1

tuples ×1

typescript ×1