简体   繁体   中英

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; }'. :

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. You can't use a logical not ! on a string.

Object.keys(obj) and obj should be typed:

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]);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM