如何将可选类型转换为非可选类型
据我所知,从types.optional(types.string)到types.optional,这是可行的:
const t = optional(types.string);
delete t.defaultValue但这似乎是非常错误的。有没有更好的方法?
发布于 2019-03-05 00:53:23
您可以使用.named(name)方法,该方法克隆给定的类型,为其提供一个新的name,并为您提供一种使用附加属性、视图、操作或覆盖原始类型中声明的属性、视图、操作或覆盖来“扩展”它的方法。
示例:
const Square = types
.model("Square",
{
width: types.number
}
)
.views(self => ({
surface() {
return self.width * self.width
}
}))
// create a new type, based on Square
const Box = Square
.named("Box")
.views(self => {
// save the base implementation of surface
const superSurface = self.surface
return {
// super contrived override example!
surface() {
return superSurface() * 1
},
volume() {
return self.surface * self.width
}
}
}))https://stackoverflow.com/questions/54235255
复制相似问题