据我了解 Record创建一个新类。那么为什么在 Flow 中进行以下类型检查:
const Person = Record({fname : null, lname: null});
const Person2 = Record({fnameMANGLED: null, lname: null});
const p : Person2 = new Person({fname: 'joe', lname: 'doe'}); // shouldn't Flow complain?
相反,当使用普通类时,以下内容不会进行类型检查(如预期):
class A{constructor() {}};
class B{constructor() {}};
const a: A = new A();
最佳答案
截至v4.0.0-rc.5
他们添加了类型 RecordOf<T>
和RecordFactory<T>
.
这是 docs 的片段关于如何使用这些流类型。
import type { RecordFactory, RecordOf } from 'immutable';
// Use RecordFactory<TProps> for defining new Record factory functions.
type Point3DProps = { x: number, y: number, z: number };
const makePoint3D: RecordFactory<Point3DProps> = Record({ x: 0, y: 0, z: 0 });
export makePoint3D;
// Use RecordOf<T> for defining new instances of that Record.
export type Point3D = RecordOf<Point3DProps>;
const some3DPoint: Point3D = makePoint3D({ x: 10, y: 20, z: 30 });
关于immutable.js - 如何输入Record类和实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41286658/