Home > Net >  how to handle Typescript error Object.ts (7053) in newer version of typescript?
how to handle Typescript error Object.ts (7053) in newer version of typescript?

Time:12-24

I have code function which is working fine one of my previous project, when I tried to copy it another project it started giving me following error. I have no idea what this mean. Can someone suggest what change is requires to make it work

my first guess is TS version or tslint.

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Object'. No index signature with a parameter of type 'string' was found on type 'Object'.ts(7053)

enter image description here

   private convertDates(object: Object) {
        if (!object || !(object instanceof Object)) {
            return;
        }

        if (object instanceof Array) {
            for (const item of object) {
                this.convertDates(item);
            }
        }

        var dateRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)$/; ///^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d )?(([ -]\d\d:\d\d)|Z)?$/;

        for (const key of Object.keys(object)) {
            const value = object[key];
            if (value instanceof Array) {
                for (const item of value) {
                    this.convertDates(item);
                }
            }

            if (value instanceof Object) {
                this.convertDates(value);
            }

            if (typeof value === 'string' && dateRegex.test(value)) {
                object[key] = new Date(value);
            }
        }
    }

CodePudding user response:

The problem is that Object.keys(object) returns string[] and not (keyof typeof object)[]. And TS cannot be sure that any string can be used to index object (for example, object['thisdoesnotexist'] does not work).

For the reason why this is expected TS behavior, read this answer

To fix it, either use Object.entries() to get both the key and value at the same time or cast key to keyof typeof object like so:

const value = object[key as keyof typeof object];

This explicitly tells the TS compiler that you can use key to index object.

CodePudding user response:

You're getting this error because Typescript becomes more strict with newer version.

Regarding to Typescript docs itself, you shouldn't use Object to type an object:

Don’t ever use the types Number, String, Boolean, Symbol, or Object These types refer to non-primitive boxed objects that are almost never used appropriately in JavaScript code.

Typescript recommends to use the type object instead but you'd have the same error in the console.

So, either you create an appropriate interface as type for your object or you can use the type Record<Keys, Type> (more here)

Constructs an object type whose property keys are Keys and whose property values are Type. This utility can be used to map the properties of a type to another type.

Your function would look like this:

private convertDates(object: Record<string, any>) {
  ...
}

I know, any is also considered a bad practice, so use appropriate typing ;)

  • Related