Issue
We use JSON files to configure some of our web applications and obviously mistakes are being made in field names etc.
Is it possible to validate the JSON file using a TypeScript definitions? Or should we migrate our configuration to other format?
We use both Intellij and VSCode for development.
Example definition:
interface AppConfig {
field1: number;
field2?: string;
field3: number[];
}
Example configuration:
{
"field": 1,
"field3": 2
}
Example validation result:
TS2322: Type '{ field: number; field3: number[]; }' is not assignable to type 'AppConfig'.
TS2322: Type 'number' is not assignable to type 'number[]'.
Solution
A solution could be to create JSON Schema files according to your TypeScript interfaces and then let the IDE perform the validation of the JSON files according to the schemata. IntelliJ offers such a validation (including auto-complete) with their JSON support.
If you don't want to create the schemata yourself, you can generate them from your TypeScript interfaces using the typescript-json-schema NPM package.
Answered By - fknx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.