TS Reset

TypeScript's Built-In
Typings, Improved

GitHub
TS Reset by Matt Pocock

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

with ts-reset

  • 👍 .json (in fetch) and JSON.parse return unknown
  • .filter(Boolean) behaves EXACTLY how you expect
  • 🥹 array.includes is widened to be more ergonomic
  • 🚀 And several more improvements!

Get Started

  • 01

    Install

    npm i -D @total-typescript/ts-reset
  • 02

    Create a reset.d.ts file in your project with these contents:

    // Do not add any other lines of code to this file!
    import "@total-typescript/ts-reset";
  • Enjoy improved typings across your entire project!

Become the
TypeScript Wizard
at Your Company

A collection of professional, exercise-driven, in-depth, self-paced TypeScript workshops for you to achieve TypeScript wizardry.

Embark on Your TypeScript Adventure
total typescript learning path

Example

// 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
  })

DOM Example

// Import from /dom to get DOM rules too!
import '@total-typescript/ts-reset/dom'
// localStorage just got safer!
localStorage.abc // unknown
// sessionStorage just got safer!
sessionStorage.abc // unknown

Installing only certain rules

By importing from @total-typescript/ts-reset, you're bundling all the recommended rules.

To only import the rules you want, you can import like so:

// Makes JSON.parse return unknown
import '@total-typescript/ts-reset/json-parse'
// Makes await fetch().then(res => res.json()) return unknown
import '@total-typescript/ts-reset/fetch'

For these imports to work, you'll need to ensure that, in your tsconfig.json, moduleResolution is set to NodeNext, Node16 or Bundler.

Below is a full list of all the rules available.

Caveats

Use ts-reset in applications, not libraries

ts-reset is designed to be used in application code, not library code. Each rule you include will make changes to the global scope. That means that, simply by importing your library, your user will be unknowingly opting in to ts-reset.

Rules

Rules we won't add