Flow: Flow library definition documentation should cover json files

Created on 20 Oct 2017  路  2Comments  路  Source: facebook/flow

Flow's documentation on creating library definitions does not mention the ability to provide the definition of a json file, which is apparently possible according to this comment.

I was hoping to find an example showing how one might provide an interface type definition for a json file containing an array of objects, such as below:

positions.json:

[
    {
        "id": "1",
        "title": "Councilmember"
   },
  {
       "id": "2",
       "title": "Mayor"
   }
]

Ideally, this page would discuss JSON files: https://flow.org/en/docs/libdefs/creation/

documentation

Most helpful comment

Update: I believe this is the code which would allow a simple json file to have a type specified via Flow's Library Definitions feature:

In flow-typed/positions.json.js:

declare module "./positions.json" {

  declare type Position = {
    id: number,
    name: string,
  };

  declare module.exports : Position[];
}

Then in the .js code, import the array from the .json file and the type (which will come from the interface definition we defined above):

import positionArray, {type Position} from './positions.json';

All 2 comments

Update: I believe this is the code which would allow a simple json file to have a type specified via Flow's Library Definitions feature:

In flow-typed/positions.json.js:

declare module "./positions.json" {

  declare type Position = {
    id: number,
    name: string,
  };

  declare module.exports : Position[];
}

Then in the .js code, import the array from the .json file and the type (which will come from the interface definition we defined above):

import positionArray, {type Position} from './positions.json';

So it works only for exact "./positions.json"? What if I import it from another location, and it becomes "../../position.json"?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

john-gold picture john-gold  路  3Comments

ghost picture ghost  路  3Comments

cubika picture cubika  路  3Comments

doberkofler picture doberkofler  路  3Comments

ctrlplusb picture ctrlplusb  路  3Comments