DEV Community

Victoria
Victoria

Posted on

Bring your TypeScript to the next level

Image description

With all my love for TypeScript, it has some annoying parts that you have to work around with custom solutions, type-checks, and functions. Which eventually can become very tedious and unmaintainable, and I did not know about any other solution to avoid it.

As a big fan of TypeScript, I am following Matt Pococ, the best TS dev I know, he worked at XState and Vercel and now creates lots of educational content about TS for a living. By exploring his free articles I discovered that he is the creator of a useful library called TS Reset.

It is not an Ad or any sort of promotion. I genuinely like his content and this library specifically.

What the description says:

Without ts-reset:
🚨 .json (in fetch) and JSON.parse both return any
🤦 .filter(Boolean) doesn't behave how you expect
😡 array.includes often breaks on readonly arrays
ts-reset smooths over these hard edges, just like a CSS reset does in the browser.

With ts-reset:
👍 .json (in fetch) and JSON.parse both return unknown
✅ .filter(Boolean) behaves EXACTLY how you expect
🥹 array.includes is widened to be more ergonomic
🚀 And several more changes!

And this is one of the examples how to use it and what it does:

// Import in a single file, then across your whole project...
import '@total-typescript/ts-reset'
// .filter just got smarter!
const filteredArray = [1, 2, undefined].filter(Boolean) // number[]
// Get rid of the any's in JSON.parse and fetch
const result = JSON.parse('{}') // unknown
fetch('/')
  .then((res) => res.json())
  .then((json) => {
    console.log(json) // unknown
  })
Enter fullscreen mode Exit fullscreen mode

Check out official documentation to learn more.
I am going to use this in my future projects by default for sure, it looks awesome and can save lots of time with guaranteed type safety. Try it out and decide if it suits you as well!

Happy coding!

Top comments (0)