Home > Software engineering >  Element implicitly has an 'any' type because expression of type 'string' can
Element implicitly has an 'any' type because expression of type 'string' can

Time:09-20

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?

CodePudding user response:

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.

CodePudding user response:

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