immutable.js - 如何输入Record类和实例?

标签 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();

最佳答案

截至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/

相关文章:

reactjs - 在 Jest 测试文件中使用流会导致未定义的测试、期望等

javascript - 自定义 fromJS immutablejs 问题

javascript - 在 Redux 中切换到 Immutable.js。对性能有何影响以及对组件语法有何影响?

javascript - 如何在 Immutable 中获取 `forEach` 次迭代内的索引

reactjs - 将 Flow.js 与 React 复合组件结合使用

javascript - 从代码库中删除 TypeScript 类型注释和断言

javascript - 流: Coercing an object-with-nullable-property to object-with-non-nullable-property?

javascript - 可选(选择性)流程检查

javascript - 比较 2 个不可变列表是否相等

javascript - 按日期排序列表 Immutable JS