我在使用Flow时遇到了以下问题
我有一个对象的类型别名,A
type A = {
B: {
C: string
}
}我想创建另一个类型别名B p.e.,它具有A中B属性的签名。
我尝试使用type B = A.B;,但它的流程抛出了以下错误:
[flow] [flow] B (Property cannot be accessed on type `A`)我找到了一个解决方案,但它有点棘手,一点也不优雅:
type A = {
B: {
C: string
}
}
//FAKE constant of type A
const fakeA : A = (null : any);
type B = typeof fakeA.B;还有其他解决方案吗?
谢谢你的帮助。
发布于 2018-01-17 03:08:46
可以使用$PropertyType帮助器
type A = {
B: {
C: string
}
}
type B = $PropertyType<A, 'B'>有关完整文档,请参阅https://flow.org/en/docs/types/utilities/#toc-propertytype。
发布于 2017-02-23 00:27:22
你就不能这么做吗?
type B = {
C: string
}
type A = {
B: B
}这并不完全是您所要求的,但它确实允许您避免重复。我不相信有一种方法可以完全做到你所要求的。
https://stackoverflow.com/questions/42232564
复制相似问题