我有一个可能很奇怪的情况,我正在尝试用 typescript 建模。
我有一堆具有以下格式的函数
type State = { something: any }
type InitialFn = (state: State, ...args: string[]) => void
我希望能够创建一个表示
InitialFn
的类型删除了第一个参数。就像是// this doesn't work, as F is unused, and args doesn't correspond to the previous arguments
type PostTransformationFn<F extends InitialFn> = (...args: string[]) => void
这可能吗?
最佳答案
我认为您可以以更通用的方式做到这一点:
type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => infer R ? (...args: P) => R : never;
接着:
type PostTransformationFn<F extends InitialFn> = OmitFirstArg<F>;
PG
关于javascript - typescript 从函数中删除第一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58764853/