[英]Typescript copying only function arguments and not return type
I have some util methods and I'm wondering if there's a way to copy arguments from one method to another. 我有一些util方法,我想知道是否有一种方法可以将参数从一个方法复制到另一个方法。 I was playing around with typeof
and trying to type the 2nd function that way but I can't quite figure it out. 我在玩typeof
并尝试以这种方式键入第二个函数,但我不太清楚。
declare function foo(a: number, b: string): number;
now I want a type bar
to have foo
's arguments, but not the return type, for example let's say it calls foo but doesn't return anything: 现在,我希望类型bar
具有foo
的参数,但不具有返回类型,例如,假设它调用foo但不返回任何内容:
const bar = (...args) => { foo(...args); }
Now I can declare bar
to have the exact same type as foo
: 现在,我可以声明bar
与foo
具有完全相同的类型:
const bar: typeof foo = (...args) => { foo(...args); }
but the return type doesn't match now. 但是返回类型现在不匹配。 So how do I either: 所以我怎么做:
typeof foo
更改我从typeof foo
获得的返回类型 There's built-in Parameters type 内置参数类型
declare function foo(a: number, b: string): number;
type fooParameters = Parameters<typeof foo>;
declare const bar: (...parameters: fooParameters) => void;
// inferred as const bar: (a: number, b: string) => void
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.