# ts-reset — A CSS Reset for TypeScript Types > A library that fixes common TypeScript type annoyances by providing stricter, more useful default types for built-in JavaScript methods like .json(), .filter(), and .includes(). ## Install Save as a script file and run: # ts-reset — A CSS Reset for TypeScript Types ## Quick Use ```bash npm install @total-typescript/ts-reset ``` ```typescript // In a global .d.ts file or entry point: import "@total-typescript/ts-reset"; // Now .json() returns unknown instead of any const data = await fetch("/api").then(r => r.json()); // data is unknown — forces you to validate // .filter(Boolean) removes falsy types correctly const arr = [1, null, 2, undefined].filter(Boolean); // arr is number[] — not (number | null | undefined)[] ``` ## Introduction ts-reset is a TypeScript type library by Matt Pocock that improves the default types for common JavaScript APIs. Much like a CSS reset normalizes browser styles, ts-reset patches overly loose TypeScript definitions so that methods like JSON.parse, fetch .json(), and Array.filter behave in a stricter, more predictable way. ## What ts-reset Does - Makes `JSON.parse()` and `Response.json()` return `unknown` instead of `any` - Fixes `.filter(Boolean)` to properly narrow types by removing falsy values - Makes `.includes()` on readonly arrays accept wider input types without casting - Widens `Map` and `Set` `.has()` to accept broader key types for lookups - Patches `Array.indexOf` and `Array.lastIndexOf` to accept supertypes ## Architecture Overview ts-reset works by shipping ambient type declaration files (.d.ts) that override specific TypeScript lib definitions via module augmentation and declaration merging. When imported, these declarations patch global interfaces like Body, JSON, Array, Map, and Set. No runtime code is emitted; the library is purely a compile-time type layer. ## Self-Hosting & Configuration - Install with `npm install @total-typescript/ts-reset` - Import once in a global `.d.ts` file or your project entry point - Choose granular resets by importing specific modules like `@total-typescript/ts-reset/json-parse` - No configuration files needed; it works automatically after import - Compatible with any TypeScript version 4.7 and above ## Key Features - Zero runtime cost: only type declarations, no JavaScript output - Granular imports let you enable only the resets you want - Prevents `any` from leaking into your codebase through standard APIs - Makes `.filter(Boolean)` a proper type guard - Accepted by the TypeScript community as a de facto best practice ## Comparison with Similar Tools - **TypeScript strict mode** — catches different issues; ts-reset fixes loose built-in lib types - **Zod / Valibot** — runtime validation; ts-reset is compile-time type patching only - **type-fest** — adds new utility types; ts-reset fixes existing built-in types - **ts-essentials** — utility types for advanced patterns; ts-reset targets standard API ergonomics - **Custom .d.ts overrides** — manual and error-prone; ts-reset is maintained and tested ## FAQ **Q: Does ts-reset add any runtime code?** A: No. It is purely a type-level library with zero runtime impact or bundle size cost. **Q: Can I use only some of the resets?** A: Yes. Import specific modules like `@total-typescript/ts-reset/filter-boolean` to apply individual fixes. **Q: Will ts-reset break existing code?** A: It makes types stricter. Code that relied on `any` from JSON.parse may need type narrowing, which is the intended improvement. **Q: Is ts-reset compatible with ESLint and Prettier?** A: Yes. It only affects TypeScript type checking and has no interaction with linting or formatting. ## Sources - https://github.com/mattpocock/ts-reset - https://www.totaltypescript.com/ts-reset --- Source: https://tokrepo.com/en/workflows/asset-3e42aab0 Author: Script Depot