返回函数的函数的Typescript接口

时间:2019-05-10 05:14:00

标签: typescript currying

我有一个StoryOptions对象,其属性之一是actionFn,该对象在被调用时将返回一个函数。 (我正在使用function currying)。

actionFn需要接受类型为ActionBundle的对象,它将返回接受Payload并返回ReduxAction的函数。

如何在打字稿中定义StoryOptions接口?

我尝试过:

interface StoryOptions {
    baseName: string; 
    actionFn(actions: ActionBundle): ((payload: Payload): ReduxAction); 
}

但这告诉我:

  

“ ReduxAction”仅引用类型,但在此处用作值。ts(2693)

2 个答案:

答案 0 :(得分:1)

您可以通过这种方式证明它是一个函数:

interface StoryOptions {
    baseName: string; 
    actionFn(actions: ActionBundle): ((payload: Payload) => ReduxAction); 
}

答案 1 :(得分:0)

应该是

interface StoryOptions {
  actionFn(actions: ActionBundle): (payload: Payload) => ReduxAction; 
}

在TS中指定可调用接口的两种方式。

type Callable = { (...args: any[]): any }
// or
type Callable = (...args: any[]) => any

使用第一语法时,必须使用周围的{ }。那就是你想念的。