[英]Typescript omit argument from generic function type
How to omit prop type from generic function?如何从通用 function 中省略道具类型?
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
in func2
how to remove property age
from first argument of FunctionType
?在func2
中如何从FunctionType
的第一个参数中删除属性age
?
I tried:我试过了:
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
This way I can remove the property age
, but the return type is no longer generic.这样我可以删除属性age
,但返回类型不再是通用的。 How I can keep it generic and omit the age
from the first argument?我如何才能保持它的通用性并从第一个参数中省略age
?
Not sure what you need that generic for since you don't seem to use it, but this should do:不确定你需要那个通用的是什么,因为你似乎没有使用它,但这应该做:
declare function func2<P extends {}, R>(f: (params: P) => R)
: (params: Omit<P, 'age'>) => R
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.