(打字稿)如何实例化包含数组类型字段的对象文字?
(Typescript) How to instanciate object literal that contain a field of array type?
我遇到以下代码的错误:
error 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 return 任意)
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"
]
}
我很难在打字稿中找到用数组描述对象文字的代码示例。
感谢您的帮助!
而不是 new Array<any>()
,使用 []
。 TypeScript 将根据变量的类型定义推断文字类型。
我遇到以下代码的错误:
error 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 return 任意)
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"
]
}
我很难在打字稿中找到用数组描述对象文字的代码示例。
感谢您的帮助!
而不是 new Array<any>()
,使用 []
。 TypeScript 将根据变量的类型定义推断文字类型。