loading...

re: Key Headaches in TypeScript VIEW POST

FULL DISCUSSION
 

Notice that the type object is not the same as the type {}

typescriptlang.org/docs/handbook/b...

The type { [key:string]: any } is indeed what you want when referring the "JS object" that is more of a hashmap or record.

without disabling TS's core strengths

any basically does that. So a better way would be to use { [key:string]: unknown }

Also, if defining it as an interface is something that bothers you (rightfully so) know that it needn't be defined as a separate type, it could be inline in the function declaration.

const getEquivalentKeys = (object1: { [key:string]: unknown }, object2: { [key:string]: unknown }): Array<string> => {
   // ...
}
 

Notice that the type object is not the same as the type {}

I'm not seeing any reference to this in the link that you provided. Also, when you define something as type object, TS's own error messages refer to it as type {}. So while I freely admit that maybe there's something that I'm just not "getting" here, it would seem that object is indeed identical to {}??

any basically does that. So a better way would be to use { [key:string]: unknown }

In this scenario it may be better, since we're just comparing the values. But as a general way to define objects, that wouldn't work, because unknown would keep us from setting any new value on the object.

Also, if defining it as an interface is something that bothers you (rightfully so) know that it needn't be defined as a separate type, it could be inline in the function declaration.

Good point. Thanks for demonstrating the shorthand.

Code of Conduct Report abuse