How to check if a property provided to a React component is an instance of Immutable.js Record with Flow?

Trying to set up Flow in my project, but I don’t quite understand how to work with immutable records. I want to statically check component details, this is how I do it:

// @flow

import React from "react";
import {render} from "react-dom";
import * as I from "immutable";

const Person = I.Record({
  name: null,
});

type Props = {
  data: Person,
};

const PersonInfo = (props: Props) => {
  const {data} = props;
  return (
    <span>
      Name: {data.name}
    </span>
  );
};

render(
  <PersonInfo data={1} />, // I would expect to get some compile error here
  document.getElementById("app")
);

I added to the project immutable.js.flowand .flowconfig.

+4
source share
1 answer

Immutable.js. any. Record typecheck. , . , Flow ( ). Record , . :

declare class Record<T: Object> {
  static <T: Object>(spec: T, name?: string): Record<T>;
  get: <A>(key: $Keys<T>) => A;
  set<A>(key: $Keys<T>, value: A): Record<T>;
  remove(key: $Keys<T>): Record<T>;
}

, (, data.name), get, data.get('name'), - , savety. , , - typechecked, .

, Flow. , , Flow.

TL; DR

Record typehecked, - . , .

+1

Source: https://habr.com/ru/post/1655711/


All Articles