我们使用JSON文件来配置一些Web应用程序,显然会在字段名称等方面出现错误。
是否可以使用TypeScript定义来验证JSON文件?或者我们应该将配置迁移到其他格式?
我们同时使用Intellij和VSCode进行开发。
示例定义:
interface AppConfig {
field1: number;
field2?: string;
field3: number[];
}
示例配置:
{
"field": 1,
"field3": 2
}
示例验证结果:
TS2322: Type '{ field: number; field3: number[]; }' is not assignable to type 'AppConfig'.
TS2322: Type 'number' is not assignable to type 'number[]'.