Javascript 使用flow注释具有多个属性的类?
我有一个具有许多成员属性的类。所有这些重复似乎都很可笑。有没有一种不那么冗长的方式来注释它Javascript 使用flow注释具有多个属性的类?,javascript,flowtype,Javascript,Flowtype,我有一个具有许多成员属性的类。所有这些重复似乎都很可笑。有没有一种不那么冗长的方式来注释它 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:
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
}
}
流中的类是,但我们可以使用type$NominalToStruct=$Exact将其转换为结构类型代码>
如果我们的类有一些额外的字段或方法,我们可以将该构造移到父类并使用super
class SpaceChild extends Space {
extraField: 1;
constructor(args: $NominalToStruct<Space>){
super(args);
}
extraMethod(){}
}
class SpaceChild扩展空间{
外场:1;
构造函数(参数:$NominalToStruct){
超级(args);
}
extraMethod(){}
}
您可以使用hack,该$ReadOnly
将表示空间
实例成员:
export default class Space {
name: string
flush_timeout: number
...
constructor(args: $ReadOnly<Space>) {
this.name = args.name
this.flush_timeout = args.flush_timeout
...
}
}
导出默认类空间{
名称:string
刷新超时:数字
...
构造函数(参数:$ReadOnly){
this.name=args.name
this.flush\u timeout=args.flush\u timeout
...
}
}
所以我知道,你的帖子真的不清楚。请澄清你到底在寻求什么帮助。我的问题是,是否有一种不太详细的方法可以使用流类型来注释Javascript类。我想我的问题很清楚。这看起来很有希望,但Flow在实例化该类时会抛出错误。看这个例子:@jkerr838,很好。使用$ReadOnly,这将是我问题的答案,但我只是将其插入TryFlow,并得到一个关于$NominalToStruct无法解决的错误。我还搜索了文档,什么也没找到。可能是从最新版本的flow中删除的?这是我在开始时描述的自定义类型:type$NominalToStruct=$Exact代码>
class SpaceChild extends Space {
extraField: 1;
constructor(args: $NominalToStruct<Space>){
super(args);
}
extraMethod(){}
}
export default class Space {
name: string
flush_timeout: number
...
constructor(args: $ReadOnly<Space>) {
this.name = args.name
this.flush_timeout = args.flush_timeout
...
}
}