【发布时间】:2020-07-08 17:44:30
【问题描述】:
以下代码出现此错误:
错误 TS2693:'any' 仅指一种类型,但在此处用作值
let modelYaml = this.readFileModel('models/model.yml');
// Build input model
let inputModel = {
elements: any[] = new Array<any>(), <--Error occured on this line
relations: any[]= new Array<any>() <--Error occured on this line
}
inputModel.elements.push(modelYaml); <- Error occured
modelYaml 是对象类型(函数 readFileModel 返回任何)
tsconfig.json
"compilerOptions": {
"baseUrl": "tsconfig",
"lib": [
"es2018",
"dom"
],
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitThis": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"rootDir": "src/",
"skipDefaultLibCheck": true,
"skipLibCheck": true,
"sourceMap": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"target": "es6",
"types": [
"jasmine",
"node"
]
}
我很难在 typescript 中找到一个用数组描述对象字面量的代码示例。
感谢您的帮助!
【问题讨论】:
-
你把类型放在错误的地方..
let inputModel: { elements: any[], relations: any[] } = { elements: [], relations: [] }。 typescriptlang.org/docs/handbook/basic-types.html -
@AlekseyL。谢谢 !问题解决了。您可以回答我的问题,我会将其标记为答案。
标签: arrays typescript object-literal