【发布时间】:2019-09-22 19:33:35
【问题描述】:
我有一个config.json 文件
{
"profiler": {
"port": 8001,
"profilerCache": {
"allowedOriginsRegex": ["^http:\/\/localhost:8080$", "i"]
}
},
"database": {
"uri": "mongodb+srv://...",
"dbName": "profiler",
"collectionName": "profiles"
}
}
在构建时,如果 json 结构与我的接口不匹配,我想查看错误
export interface Config {
port: number
profilerCache: {
allowedOriginsRegex: [string, string]
}
database: {
uri: string
dbName: string
collectionName: string
}
}
为我的 json 文件实现类型安全的最简单方法是什么?
【问题讨论】:
-
你可以创建一个像
let x: Config = your-json这样的临时文件并编译它,参见例如stackoverflow.com/a/57942560/989121
标签: json typescript