这是我的代码的一部分:
const myObj: object = {}
const propname = 'propname'
myObj[propname] = 'string'
Run Code Online (Sandbox Code Playgroud)
但我得到了错误:
ERROR in path/to/file.ts(4,1)
TS7053: Element implicitly has an 'any' type because expression of type '"propname"' can't be used to index type '{}'.
Property 'propname' does not exist on type '{}'.
Run Code Online (Sandbox Code Playgroud)
这有什么问题,我该如何解决?
Phi*_*ipp 51
TS7053 Element implicitly has an 'any' type以下是解决通过数组访问访问属性时出现“ ”错误的一些解决方案。
原始代码:
const myObj: object = {}
const prop = 'propname'
myObj[prop] = 'string' // Error!
Run Code Online (Sandbox Code Playgroud)
注意:这不起作用,因为索引签名仍未定义:
const myObj: {propname: any} = {}
const prop = 'propname'
myObj[prop] = 'string' // Error!
Run Code Online (Sandbox Code Playgroud)
解决方案 1:隐式定义索引签名
const myObj: {[key: string]: any} = {}
const prop = 'propname'
myObj[prop] = 'string'
Run Code Online (Sandbox Code Playgroud)
解决方案 2:使用接口提供索引签名
interface IStringIndex {
[key: string]: any
}
const myObj: IStringIndex = {}
const prop = 'propname'
myObj[prop] = 'string'
Run Code Online (Sandbox Code Playgroud)
解决方案 3:使用接口并扩展<Record>实用程序类型:
interface IStringIndex extends Record<string, any> {}
const myObj: IStringIndex = {}
const prop = 'propname'
myObj[prop] = 'string'
Run Code Online (Sandbox Code Playgroud)
解决方案 4:使用索引签名定义类型别名
type MyObject = {
[key: string]: any
propname?: any
}
const myObj: MyObject = {}
const prop = 'propname'
myObj[prop] = 'string'
Run Code Online (Sandbox Code Playgroud)
解决方案 5:组合描述索引签名的接口和描述有效属性的类型别名:
interface IStringIndex extends Record<string, any> {}
type MyObject = IStringIndex & {
propname?: string
}
const myObj: MyObject = {}
const prop = 'propname'
myObj[prop] = 'string'
Run Code Online (Sandbox Code Playgroud)
解决方案 6:定义有效(字符串)属性名称的列表:
type ValidProps = 'propname' | 'value'
interface IStringIndex extends Record<ValidProps, any> {}
const myObj: IStringIndex = {
propname: 'my prop',
value: 123
}
const prop = 'propname'
myObj[prop] = 'string'
Run Code Online (Sandbox Code Playgroud)
注意:ValidProps分配对象时,列表中的所有属性都必须存在!
pop*_*pop 26
如果您定义了接口/类,但它仍然抛出该错误,您可以使用keyof如下方法:
interface SomeInterface {
propertyA: string;
propertyB: string;
}
const object: SomeInterface = {propertyA: 'A', propertyB: 'B'};
for ( const prop in object ) {
const value = object[prop]; // <-- will throw an error
const typedValue = object[prop as keyof SomeInterface]; // <-- will do fine
}
Run Code Online (Sandbox Code Playgroud)
否则,您可以通过向要通过的接口/类添加泛型属性来“破解”它,但它为类型不安全打开了一扇完全不同的门:)
interface SomeInterface {
propertyA: string;
propertyB: string;
[key: string]: any;
}
Run Code Online (Sandbox Code Playgroud)
Mur*_*göz 10
您必须定义对象具有哪种索引类型。在您的情况下,它是一个string基于索引的索引。
const myObj: {[index: string]:any} = {}
Run Code Online (Sandbox Code Playgroud)
const myObj: object = {'foo': 'bar'}
const 属性名 = 'foo';
(myObj as any)[propname] = 'baz';
Run Code Online (Sandbox Code Playgroud)
小智 7
转到:tsconfig.json,并设置以下选项
"compilerOptions": {
"noImplicitAny": false,
}
const listOfArray: Array<any> = [];
for(const Id in MyObject)
{
listOfArray.push(dataList[Id]);//TS7053 Error here -- Fixed by doing above things
}
Run Code Online (Sandbox Code Playgroud)
当我尝试推送来自 的结果时,面临同样的错误MyObject [{Id: value 1},{Id: 2},{Id: 3}]。
| 归档时间: |
|
| 查看次数: |
4400 次 |
| 最近记录: |