【发布时间】:2021-12-24 17:44:44
【问题描述】:
我有一个 TypeScript 项目,我试图从属性文件中提取数据,但它显示了以下错误,我无法在 getProperty[codProduct.toUpperCase()] 代码中解决:
The element has a type "any" implicitly because the expression of type "string" cannot be used to index the type {}
这是我的功能:
import * as logger from 'winston';
import * as getProperty from '../json/product.json';
import { ListProductBatchModel, ResultMsg } from '../models/listProduct.models';
export class ListProductBatchUtils {
public async getDataProduct(codProduct: string): Promise<ListProductBatchModel> {
try {
let result: ResultMsg;
if (getProperty[codProduct.toUpperCase()]) {
result = {
name: getProperty[codProduct.toUpperCase()].name,
price: getProperty[codProduct.toUpperCase()].price
}
}else {
result = {
error: "ERROR"
}
}
logger.info('start')
return Promise.resolve({ resp: result });
} catch (error) {
logger.info(JSON.stringify(error));
return Promise.reject(error);
}
}
}
这是我的属性文件:
{
"CT": {
"name": "car",
"price": "5,00"
},
"CC": {
"name": "box",
"price": "6,00"
}
}
这是models.ts:
interface ListProductBatchModel {
resp: ResultMsg
}
interface ResultMsg {
name?: string,
price?: string,
error?: string
}
export { ListProductBatchModel, ResultMsg };
【问题讨论】:
标签: arrays json typescript properties