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/
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"?
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: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';