【问题标题】:Is it possible to create an interface in a typescript method?是否可以在打字稿方法中创建接口?
【发布时间】:2021-02-02 04:09:16
【问题描述】:

小注:Is it possible to include method in TypeScript interface? 可能看起来很相似,但它问的是这个问题的反面。

我有一个 typescript 方法,它使用 options 参数,可以正常工作和编译:

  async addKickingRule(options: KickingRuleOptions = {
    time: 60 * MINUTES,
    privileges: ALL_PRIVILEGES
  }) {
    ...
  }

它使用在别处定义的接口:


interface KickingRuleOptions {
  time: number,
  privileges: Privileges[],
  channel?: string,
  userID?: string,
  ipAddress?: string
}

同样,这很好用。但是,我觉得如果在方法体中定义接口,对我的同事来说会更容易。

我可以在 typescript 方法中使用 options 对象并在方法体中定义选项接口吗?

【问题讨论】:

    标签: node.js typescript oop interface


    【解决方案1】:

    你可以这样做:

    async addKickingRule(options: {
      time: number,
      privileges: Privileges[],
      channel?: string,
      userID?: string,
      ipAddress?: string
    } = {
      time: 60 * MINUTES,
      privileges: ALL_PRIVILEGES
    }) {
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-07
      • 2016-02-26
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 2019-07-27
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多