typescript 类型脚本省略泛型函数类型的参数

ih99xse1  于 2023-01-06  发布在  TypeScript
关注(0)|答案(1)|浏览(145)

如何从泛型函数中省略prop类型?

declare function func1<T extends {...}>(params: {age: number, something: T, ...more}): string

declare function func2<FunctionType extends (...args: any) => any>(cb: FunctionType): FunctionType
const newFunction = func2(func1)  // returns same type as func1
newFunction<{...}>({something: {...}, age: ...}) // is valid, but age should not be valid

func2中,如何从FunctionType的第一个参数中删除属性age
我试过:

declare function func2<FunctionType extends (...args: any) => any>(cb: FunctionType): 
(params: Omit<Parameters<FunctionType>[0], 'age'>) => ReturnType<FunctionType>

const newFunction = func2(func1)  // This returns the new function type without age, but also without generics
newFunction<{...}>({...})   // the generic is no longer valid, bit is should be

这样我就可以删除属性age,但是返回类型不再是泛型的。我怎样才能保持它的泛型并从第一个参数中省略age呢?

xnifntxz

xnifntxz1#

不知道你需要这个通用的,因为你似乎不使用它,但这应该做:

declare function func2<P extends {}, R>(f: (params: P) => R)
   : (params: Omit<P, 'age'>) => R

相关问题