How can I type data from a JSON file in TypeScript?
Simple answer:
- enable it in the TS config.
"compilerOptions": {
"resolveJsonModule": true,
}
- Pull the json prop into type def:
type sampleProp = typeof jsonDataProp;
When you want to ensure that the JSON shows up as an optional prop (maybe defined later etc):
type UcpConfig = Partial<GeneratedUcpConfig>;
Tells TS that GeneratedUcpConfig might have props on it, or might be {}
.
More reading
https://dev.to/nickytonline/strongly-typed-json-in-typescript-5gb2