我有一个包含许多成员属性的类。所有这些重复似乎很荒谬。有没有更简洁的方法来注释它?
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
}
}
最佳答案
你可以使用一个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
...
}
}
关于javascript - 使用流注释具有许多属性的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53177385/