DEV Community

Discussion on: TypeScript Utility Types: The 6 Most Useful

peerreynders profile image

I f you want to construct an object type with a set of properties keys of type types then the Record is the best utility type to use.

Even the TypeScript documentation is sloppy here.

Ultimately Records are always based on plain JavaScript objects:

"All keys in the square bracket notation are converted to strings, unless they're Symbols. JavaScript object property names (keys) can only be strings or Symbols. … JavaScript will call the anotherObj.toString() method, and use this result string as the new key."

So TypeScript will also allow string | number as a Key type but the distinct values 10 and "10" will always map to the same key "10".

If it is necessary to use types other than string or Symbol as a Key consider using the Map type instead:

"A Map's keys can be any value (including functions, objects, or any primitive)."