首页 > 解决方案 > 使用流注释具有许多属性的类?

问题描述

我有一个包含许多成员属性的类。所有这些重复似乎都很荒谬。有没有更简洁的注释方式?

type Args = {
  name: string,
  flush_timeout: number,
  close_timeout: number,
  slab_threshold: number,
  slab_fanout: number,
  max_writers: number,
  min_time: EpochObj,
  max_time: EpochObj,
  size: number,
  packet_support: boolean,
  compression: string,
  path: string
}

export default class Space {
  name: string
  flush_timeout: number
  close_timeout: number
  slab_threshold: number
  slab_fanout: number
  max_writers: number
  min_time: EpochObj
  max_time: EpochObj
  size: number
  packet_support: boolean
  compression: string
  path: string

  constructor(args: Args) {
    this.name = args.name
    this.flush_timeout = args.flush_timeout
    this.close_timeout = args.close_timeout
    this.slab_threshold = args.slab_threshold
    this.slab_fanout = args.slab_fanout
    this.max_writers = args.max_writers
    this.min_time = args.min_time
    this.max_time = args.max_time
    this.size = args.size
    this.packet_support = args.packet_support
    this.compression = args.compression
    this.path = args.path
  }
}

标签: javascriptflowtype

解决方案


您可以使用hack$ReadOnly<Space>它将代表Space实例成员:

export default class Space {
  name: string
  flush_timeout: number
  ...

  constructor(args: $ReadOnly<Space>) {
    this.name = args.name
    this.flush_timeout = args.flush_timeout
    ...
  }
}

推荐阅读