如何输入记录类和实例?

时间:2016-12-22 15:33:52

标签: immutable.js flowtype

我知道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();

2 个答案:

答案 0 :(得分:3)

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 });

答案 1 :(得分:0)

这是因为在不可变JS的当前类型定义中,Record factory constructor返回any

如果您取消注释/*T & Record<T>*/catches the error as expected