How to Make Your TypeScript Generic Functions Safer and Easier to Use | by Dmitry Shamshurin | Mar, 2022

Picture by Mohammad Rahmani on Unsplash

Anyway right here we see two principal issues:

  1. We have now no arguments security — we will go ‘blah’ and ‘blahblah’ as parameters to our mapByKey operate and Typescript received’t cease us from it.
  2. We have now to do the Type assertions to “calm” Typescript down, including a considerable threat of an error. It is because TS will deal with the consequence variable as a Map of any T[keyof T] to any T[keyof T] (on this instance T[keyof T] could be string | Date | title: string | quantity), and any sort assertion that satisfies these sorts might be “authorized”.

More Posts