简体   繁体   English

推断 typescript function arguments

[英]Infer typescript function arguments

Given the following鉴于以下

const action1 = (arg1: string) => {}
const action2 = (arg1: string, arg2: {a: string, b: number}) => {}
const actions = [action1, action2]
handleActions(actions)

... elsewhere ...

const handleActions = (actions: WhatTypeIsThis[]) => {
  const [action1, action2] = actions;
  action1(/** infer string */)
  action2(/** infer string and object */)
}

How can I define the WhatTypeIsThis type in order for the action args to be inferable inside handleActions ?如何定义WhatTypeIsThis类型以使操作 args 在handleActions中可推断?

Is it possible to define it in such a way that actions can be any number of functions with varying argument lists?是否可以将其定义为actions可以是具有不同参数列表的任意数量的函数?

Is it possible using generics?是否可以使用 generics?

Is it possible to define it in such a way that actions can be any number of functions with varying argument lists?是否可以将其定义为动作可以是具有不同参数列表的任意数量的函数?

With a dynamic list, you need runtime checking.使用动态列表,您需要运行时检查。 I don't think you can do runtime checking on these functions without branding them (I tried doing it on length since those two functions have different lengths, but it didn't work and it really wouldn't have been useful even if it had — (arg1: string) => void and (arg1: number) => void are different functions with the same length ).我不认为你可以在不标记它们的情况下对这些函数进行运行时检查(我尝试对length进行检查,因为这两个函数有不同的长度,但它不起作用,即使它确实没有用— (arg1: string) => void(arg1: number) => void是具有相同length的不同函数)。

With branding and a runtime check, it's possible:通过品牌和运行时检查,可以:

  • Define branded types for the functions定义功能的品牌类型
  • Create the functions创建函数
  • Define the action list as an array of a union of the function types将动作列表定义为 function 类型的联合数组
  • Have handleActions branch on the brand在品牌上有handleActions分支

Like this:像这样:

type Action1 = (
    (arg1: string) => void
) & {
    __action__: "action1";
};

type Action2 = (
    (arg1: string, arg2: {a: string, b: number}) => void
) & {
    __action__: "action2";
};

const action1: Action1 = Object.assign(
    (arg1: string) => {},
    {__action__: "action1"} as const
);
const action2: Action2 = Object.assign(
    (arg1: string, arg2: {a: string, b: number}) => {},
    {__action__: "action2"} as const
);

const actions = [action1, action2];

type ActionsList = (Action1 | Action2)[];

const handleActions = (actions: ActionsList) => {
    const [action1, action2] = actions;
    if (action1.__action__ === "action1") {
        action1("x");   // <==== infers `(arg: string) => void` here
    }
};  

handleActions(actions);

Playground link 游乐场链接


Before you added the text quoted at the top of the answer, it was possible with a readonly tuple type .添加答案顶部引用的文本之前,可以使用 readonly tuple type I'm keeping this in the answer in case it's useful to others, even though it doesn't apply to your situation.我将其保留在答案中,以防它对其他人有用,即使它不适用于您的情况。

Here's what that looks like:看起来是这样的:

type ActionsList = readonly [
    (arg1: string) => void,
    (arg1: string, arg2: { a: string; b: number; }) => void
];

To make it readonly, you'll need as const on actions :要使其只读,您需要as const on actions

const actions = [action1, action2] as const;
//                                ^^^^^^^^^

A tuple is a kind of "...Array type that knows exactly how many elements it contains, and exactly which types it contains at specific positions."元组是一种“...数组类型,它确切地知道它包含多少元素,以及它在特定位置包含哪些类型。” (from the link above) (来自上面的链接)

Playground link 游乐场链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript:推断 arguments 的回调 function 作为参数传递给另一个 ZC1C425268E68385D1AB504FC - Typescript: Infer arguments of a callback function passed as a parameter to another function Typescript function 作为参数 - 使用输入的 ZDBC11CAA5BDA28F77E6FB4EDABD8 推断 output 类型 - Typescript function as argument - infer output type using inputted arguments Typescript泛型:从函数参数的类型推断类型? - Typescript generics: infer type from the type of function arguments? 打字稿:正确推断包装函数的参数和返回类型 - Typescript: correctly infer arguments and return type for wrapped function 为什么 Typescript 不能推断出可选 arguments 的 function 参数类型? - Why can't Typescript infer function argument types for optional arguments? 推断打字稿中抽象类的参数 - Infer arguments for abstract class in typescript Typescript - 推断 function 参数 - Typescript - infer function parameter 打字稿:推断函数参数 - Typescript: Infer function argument 我如何让 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 如何为 function 推断 arguments 的类型? - How to infer type for arguments for function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM