javascript - 使用流注释具有许多属性的类?
问题描述
我有一个包含许多成员属性的类。所有这些重复似乎都很荒谬。有没有更简洁的注释方式?
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
...
}
}
推荐阅读
- mysql - MYsql查询底部显示数量为0的产品
- javascript - 具有封闭 shadowRoot 的登录表单需要保护
- c# - 为什么 ASP.NET 在抛出 HttpRequestValidationException 时不会在 Global.asax.cs 中输入 Application_Error?
- storyboard - 第一个故事板要显示
- python-3.x - 静默安装 Python
- sql - 根据条件过滤掉某些行
- html - CSS在表单标签中不起作用
- postgresql - 使用 EF Core 和 Npgsql 查询行(复合)值
- kubernetes - 如何使用 Helm 从列表中迭代创建 pod?
- javascript - TypeScript 无法检测 *.vue 文件