gitmyhub

ts-reset

TypeScript ★ 8.5k updated 2mo ago

A 'CSS reset' for TypeScript, improving types for common JavaScript API's

ts-reset is a tiny TypeScript add-on that fixes three built-in frustrations: JSON.parse returning untyped results.filter(Boolean) not narrowing array types correctly, and type errors when using .includes() on readonly arrays.

TypeScriptsetup: easycomplexity 1/5

ts-reset is a small TypeScript library that fixes several frustrating quirks in TypeScript's built-in type definitions for everyday JavaScript features. TypeScript is a programming language that adds type-checking on top of JavaScript, helping catch mistakes before code runs. However, some of the types that TypeScript ships with for standard JavaScript tools are intentionally loose or produce awkward behavior in practice.

The library takes its name from the concept of a CSS reset, a small stylesheet that browser developers use to neutralize inconsistent default styles before writing their own. ts-reset does the same thing for TypeScript: it patches a handful of built-in definitions so they behave more sensibly without you having to write workarounds yourself.

The README lists three specific problems it addresses. First, when you fetch data from an API and parse the response as JSON, or when you call JSON.parse directly, TypeScript currently types the result as any, meaning it skips type-checking entirely. ts-reset changes this to unknown, which forces you to check what you actually received before using it. Second, a common JavaScript pattern of filtering an array to remove empty values does not narrow the type correctly in standard TypeScript, but ts-reset makes it work as most developers would expect. Third, checking whether a value is in a readonly array with array.includes often causes a type error; ts-reset widens the check to remove that friction.

The README is brief and points to full documentation on the Total TypeScript website for installation instructions and a complete list of changes. The project was created by Matt Pocock, a well-known TypeScript educator.

Where it fits