【问题标题】:Typescript Interface make optional打字稿界面使可选
【发布时间】:2018-02-04 22:44:44
【问题描述】:

我有一个这样的界面:

export interface IDefaultAction extends Object {
  type: string
  (dispatch: Dispatch<IStateObject>, getState: () => IStateObject, extraArgument: any): any;
}

有什么方法可以让界面中的第二行变成可选的吗? (dispatch: Dispatch&lt;IStateObject&gt;, getState: () =&gt; IStateObject, extraArgument: any): any;

如果是这样,怎么做?

如果可能的话,请解释或指出正确的文档来解释这个接口的含义:

interface IA {
  ():any;
}

我就是想不通这个语法

():something;

谢谢!

编辑:

我正在尝试扩展它:

export type ThunkAction<R, S, E> = (dispatch: Dispatch<S>, getState: () => S,
                                    extraArgument: E) => R;

在我自己的界面中:

export interface IDefaultAction {
  type: string;
}

但可选地, 所以我唯一能想到的就是修改原始的(ThunkAction) 并使其里面的所有内容都是可选的,但我不知道如何。

【问题讨论】:

    标签: typescript typescript-typings typescript2.0


    【解决方案1】:

    请解释或指向正确的文档,该文档解释了此接口的含义:

    IA 接口是一个函数接口。它定义了a "function type"

    interface IA {
        (): any;
    }
    
    const exampleImplementation: IA = () => "";
    

    (): any 定义函数类型的签名,包括函数的参数列表和返回类型。函数类型IA 不带参数并返回any

    有什么办法可以让界面中的第二行变成可选的吗?

    第二行是函数签名,表示接口定义了一个函数类型。它的函数签名接受两个参数并返回一个any

    export interface IDefaultAction extends Object {
      type: string;
      (
        dispatch: Dispatch<IStateObject>,                  // paramater 1
        getState: () => IStateObject, extraArgument: any   // parameter 2
      ) : any;                                             // return type
    }
    

    虽然接口支持optional properties,但接口不支持可选函数签名。

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 2018-10-03
      • 2019-02-07
      • 2016-06-21
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2018-05-25
      • 2019-02-06
      相关资源
      最近更新 更多