简体   繁体   English

Typescript仅复制函数参数而不返回类型

[英]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 : 现在,我可以声明barfoo具有完全相同的类型:

const bar: typeof foo = (...args) => { foo(...args); }

but the return type doesn't match now. 但是返回类型现在不匹配。 So how do I either: 所以我怎么做:

  • Just copy the argument signature 只需复制参数签名
  • Change the return type I get from 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.

相关问题 打字稿:只键入函数的参数或返回类型 - Typescript: typing only the arguments or the return type of a function Typescript 函数根据可选参数返回类型 - Typescript function return type based on optional arguments 如何基于参数中的函数返回类型指定打字稿条件类型 - How to specify typescript conditional type based on a function return type in the arguments Typescript function 返回类型取决于 arguments 的数量或类型 - Typescript function return type depending on number or type of arguments 在TypeScript中,如何键入函数的参数而不是返回值? - In TypeScript, how do i type a function's arguments but not the return value? 使用 TypeScript 使函数的返回类型取决于其参数? - Make return type of function depends on its arguments with TypeScript? 打字稿:正确推断包装函数的参数和返回类型 - Typescript: correctly infer arguments and return type for wrapped function 仅通过接口 TypeScript 键入函数的 arguments - Type function's arguments only via Interface TypeScript 我如何让 typescript 根据 function2E977777777777777777777777777777777777777777777777777777777777777777777777777777777777BED18 的返回类型 functionC17A94F14Z 的返回类型来推断 function 的返回类型 - How do I get typescript to infer the return type of a function based on the return type on a function of its arguments 在 TypeScript 中将类型作为函数返回 - return type as function in TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM