在redux中,状态应该是不可变的。我希望Flow能阻止任何人改变这种状态。因此,给定一个任意深度的对象:
type object = {
a: {
b: {
d: string
}
},
c: number
}
如何创建递归只读的新类型,以便我不能执行:
let TestFunction = (param: $RecursiveReadOnly<object>) => {
param.a.b.d = 'some string'
}
Flow的内置$ReadOnly
实用程序会创建一个这样的类型,不是需要什么,因为b
&amp; d
仍然可写:
{
+a: {
b: {
d: string
}
},
+c: number
}
我一直试图使用$Call
&amp; $ObjMap(i)
,但我无法弄清楚如何在Flow中递归移动对象。目标是:
{
+a: {
+b: {
+d: string
}
},
+c: number
}
答案 0 :(得分:4)
感谢kalley的解决方案。根据我的理解,kalley试图使函数接收的任何对象以递归方式只读。因为我真的只需要已知对象作为参数,所以这很有效:
// Type definition that works with arbitrary nested objects/arrays etc.
declare type RecursiveReadOnly<O: Object> = $ReadOnly<$ObjMap<O, typeof makeRecursive>>
declare type RecursiveReadOnlyArray<O: Object> = $ReadOnlyArray<$ReadOnly<$ObjMap<O, typeof makeRecursive>>>
type Recursive<O: Object> = $ObjMap<O, typeof makeRecursive>
declare function makeRecursive<F: Function>(F): F
declare function makeRecursive<A: Object[]>(A): $ReadOnlyArray<$ReadOnly<Recursive<$ElementType<A, number>>>>
declare function makeRecursive<O: Object>(O): RecursiveReadOnly<O>
declare function makeRecursive<I: string[] | boolean[] | number[]>(I): $ReadOnlyArray<$ElementType<I, number>>
declare function makeRecursive<I: string | boolean | number | void | null>(I): I
// Usage example.
type obj = {
a: {
b: {
d: string,
}
}
}
let TestFunction = (param: RecursiveReadOnly<obj>) => {
param.a.b.d = 'some string' // Flow throws an error
}