简体   繁体   English

在TypeScript中,如何键入函数的参数而不是返回值?

[英]In TypeScript, how do i type a function's arguments but not the return value?

I am writing an application using redux, redux-thunk and reselect in TypeScript. 我正在使用Redux,redux-thunk和在TypeScript中重新选择编写应用程序。

In many places, i am writing functions like this: 在许多地方,我正在编写如下函数:

const selectThing = (store: IStore) => store.path.to.thing;

const fetchThing = (thingId: string) => (dispatch: Dispatch<IStore>, getState: () => IStore) => {
  // fetch a thing by id and handle the result

  return result;
}

Especially in the second example, the typing annotations for the second function take up much space and i would like to write a function interface which handles typing the arguments. 尤其是在第二个示例中,第二个函数的类型注释会占用很多空间,我想编写一个处理参数类型的函数接口。

type StoreSelector<T = any> = (store: IStore) => T;
type ThunkDispatch<T = any> = (dispatch: Dispatch<IStore>, getState: () => IStore) => T;

The typings above solve the issue of manually having to type the parameters each time, but they will require me to manually type the return value of the functions, which worked automatically before. 上面的键入解决了每次都必须手动键入参数的问题,但是它们将要求我手动键入函数的返回值,该函数以前会自动工作。

Is there a way to type a function's arguments, but let typescript then automatically detect the return value of the function body? 有没有办法键入函数的参数,但是让Typescript然后自动检测函数体的返回值?

You can use a function to get inference for the return type and inference for the parameter types. 您可以使用函数来获取返回类型的推论和参数类型的推论。

function createThunkDispatch<T>(fn: (dispatch: Dispatch<IStore>, getState: () => IStore) => T) {
    return fn;
} 

// const fetchThing: (thingId: string) => (dispatch: Dispatch<IStore>, getState: () => IStore) => string
const fetchThing = (thingId: string) => createThunkDispatch((dispatch, getState) => {
    // fetch a thing by id and handle the result

    return "result";
});

暂无
暂无

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

相关问题 在TypeScript中,如何根据其参数之一的返回类型来确定函数的返回类型? - In TypeScript, how do I make a function's return type based on the return type of one of its arguments? 我如何让 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 根据调用 arguments 推断函数返回值的类型? - Is there any way to have TypeScript infer the type of a function's return value based on the call arguments? 在Typescript中,我如何声明一个返回字符串类型数组的函数? - In Typescript how do I declare a function that return string type array? 如何基于参数中的函数返回类型指定打字稿条件类型 - How to specify typescript conditional type based on a function return type in the arguments 如何在Typescript中为私有类成员声明参数和返回类型? - How do I declare the arguments and a return type for a private class member in typescript? TypeScript 映射类型改变值函数的返回类型 - TypeScript mapped type change value function's return type 打字稿:只键入函数的参数或返回类型 - Typescript: typing only the arguments or the return type of a function Typescript仅复制函数参数而不返回类型 - Typescript copying only function arguments and not return type Typescript 函数根据可选参数返回类型 - Typescript function return type based on optional arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM