简体   繁体   English

元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型样式

[英]Element implicitly has an 'any' type because expression of type 'string' can't be used to index type style

I don't know how to resolve this error:我不知道如何解决此错误:

"Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ prop_first: string; prop_second: string; }'. No index signature with a parameter of type 'string' was found on type '{ prop_first: string; prop_second: string; }'. : “元素隐式具有'any'类型,因为'string'类型的表达式不能用于索引类型'{ prop_first:string; prop_second:string;}'。没有找到带有'string'类型参数的索引签名在类型 '{ prop_first: string; prop_second: string; }' 上。:

const users = [
  { 'user': 'b', 'age': 12, 'active': true, warnings: [{propertyA: true, propertyB: false}] },
  { 'user': 'a',   'age': 22, 'active': false, warnings: [{propertyB: false}] }
];

const obj = {
   propertyA: 'true',
   propertyB: 'true',
};

Object.keys(obj).forEach((key, index) => {
  obj[key] = !(!!obj[key]);
});

console.log(obj);

const propertyA = true

const properties = {propertyA: true, propertyB: false}
 
const result = users.filter(user => user.warnings.length && _.some(user.warnings, obj))
console.log(result)
console.log(!!undefined)

any ideas?有任何想法吗?

The error is in this line:错误在这一行:

obj[key] = !(!!obj[key]);

Sometimes the value of key is a sting.有时key的价值是一种刺痛。 You can't use a logical not !你不能使用逻辑 not ! on a string.在一个字符串上。

Object.keys(obj) and obj should be typed: Object.keys(obj)obj应键入:

const obj: Record<PropertyKey, string | boolean> = {
   propertyA: 'true',
   propertyB: 'true',
};

(Object.keys(obj) as Array<keyof typeof obj>).forEach((key, index) => {
  obj[key] = !(!!obj[key]);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“BGColors” - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'BGColors' 如何修复 Element 隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型? - how fix Element implicitly has an 'any' type because expression of type 'string' can't be used to index type? 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{}” - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}' 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - Phaser - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type - Phaser 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{ AT: number; BE:数字,...}` - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ AT: number; BE: number,…}` 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 A - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type A TypeScript - 元素隐式具有“任意”类型,因为“字符串”类型的表达式不能用于索引类型 - TypeScript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type TypeScript - ts(7053):元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - TypeScript - ts(7053) : Element implicitly has an 'any' type because expression of type 'string' can't be used to index TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM