How to Make Your TypeScript Generic Functions Safer and Easier to Use
Tags: tutorial, programming, typescript, javascript
Published: 2022-03-31 19:43:36

Correctly working with types in Typescript might be very confusing (if not maddening and mind-blowing) for someone who comes from the JavaScript world. Our company has started migrating from JavaScript to TypeScript not so long ago, so most of the developers are still learning how to work with it (including myself). For some people this article might be trivial, but it was a real challenge for me. And I believe that sharing experience and knowledge about challenging things is the best we can do because it could help someone with the same challenge.

## Problem

So, once, when doing a code review of a merge request from another team, my colleague and I stumbled upon these two peculiar functions:
{% gist https://gist.github.com/V1raNi/10b78ccb1018dc4fcaac5c08094451d5.js %}
Let’s skip the review of their actual code, it’s not really relevant for this article, they are essentially transformers of objects to Maps of values of a certain object by this object keys. It’s not the insides of the functions that caught our attention, but the way those functions use the TS types. I, along with my colleague, immediately saw the flaws of these functions’ typing thanks to our previous experience in Kotlin and C++ respectively.

The functions were supposed to be used something like this:
{% gist https://gist.github.com/V1raNi/756c5175ca58eca09273b50a1044860e.js %}
It’s the real function from the same merge request, but the names and the object structure have been changed — sorry, NDA makes me do that ☹️

### Anyway here we see two main problems:

 1. We have no arguments safety — we can pass ‘blah’ and ‘blahblah’ as parameters to our mapByKey function and Typescript won’t stop us from it.

 2. We have to do the [Type assertions](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions) to “calm” Typescript down, adding a substantial risk of an error. This is because TS will treat the result variable as a Map of any T[keyof T] to any T[keyof T] (in this example T[keyof T] would be string | Date | { name: string } | number), and any type assertion that satisfies those types will be “legal”.

If you are not sure what this all means, I recommend you to read some Typescript documentation on [keyof](https://www.typescriptlang.org/docs/handbook/2/keyof-types.html) operator and [generics](https://www.typescriptlang.org/docs/handbook/2/generics.html).

## Partial Solution

The first solution that comes to our mind is to use not the string type in our mapByKey function, but keyof T:
{% gist https://gist.github.com/V1raNi/d051aa2af9db0c70cca5f588ac669338.js %}
It will definitely help Typescript stop us from making, for example, a spelling mistake:
{% gist https://gist.github.com/V1raNi/3429b1afc2db85904b947abb463ec44d.js %}
Looks better already, but what do we do about the second problem? How do we get rid of this type assertion and make the code safer? How do we “tell” Typescript that the resulting Map in those function contains not some abstract keyof T elements, but the types that we know with 100% certainty (because we know what “keys” of an object we passed to the function, and we know their types from the object structure).

## The Real Solution

After some digging and brainstorming (and long moments of despair) we finally found the solution — we need to add more generic parameters and use [type parameters with constraints](https://www.typescriptlang.org/docs/handbook/2/generics.html#using-type-parameters-in-generic-constraints). This way Typescript will be able to know the exact types of the function arguments. Our functions will now look like this:
{% gist https://gist.github.com/V1raNi/60f2accb63c01f3d30a277ccfe62c531.js %}
And now it becomes so much easier and safer to use them:
{% gist https://gist.github.com/V1raNi/a8817eb2c50db85bf40bad5f92289b3d.js %}
Now Typescript is on guard and is even helping us to write the code!

## Conclusion

Generics and other concepts might be scary, but try to make Typescript your friend, and not your enemy. Explore the Typescript [documentation](https://www.typescriptlang.org/docs/) and other resources extensively, don’t be afraid to try out stuff, and don’t get demotivated when something doesn’t work. In the end it will absolutely work out. And when it does — it will do wonders for you.

***
This is my first article, and I'm planning to write more, so any feedback is very much appreciated!

Photo by [Mohammad Rahmani](https://unsplash.com/@afgprogrammer?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/javascript?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)