A "CSS reset" for TypeScript that improves types for common JavaScript APIs.
Source: mattpocock/ts-reset (8.6K stars)
The Problem
TypeScript's built-in types have sharp edges:
| Issue | Without ts-reset | With ts-reset |
|---|---|---|
JSON.parse() | Returns any | Returns unknown |
fetch().json() | Returns any | Returns unknown |
.filter(Boolean) | Keeps nullish types | Removes nullish types |
array.includes() | Breaks on readonly | Works ergonomically |
Installation
npm install -D @total-typescript/ts-reset
Then create a reset.d.ts file (or add to existing):
// reset.d.ts
import "@total-typescript/ts-reset";
Or import specific rules:
// Only JSON.parse fix
import "@total-typescript/ts-reset/json-parse";
// Only filter(Boolean) fix
import "@total-typescript/ts-reset/filter-boolean";
What It Fixes
1. JSON.parse Returns unknown
Before:
const data = JSON.parse(input);
// data: any — no type safety!
data.foo.bar.baz; // No error, disaster waiting
After:
const data = JSON.parse(input);
// data: unknown — must validate!
if (isMyType(data)) {
data.foo; // Safe access
}
2. fetch().json() Returns unknown
Before:
const res = await fetch("/api/user");
const user = await res.json();
// user: any
user.name.toUpperCase(); // Runtime error if no name
After:
const res = await fetch("/api/user");
const user = await res.json();
// user: unknown
// Must validate first
const parsed = userSchema.parse(user);
parsed.name.toUpperCase(); // Safe!
3. filter(Boolean) Removes Nullish
Before:
const arr = [1, null, 2, undefined, 3];
const filtered = arr.filter(Boolean);
// filtered: (number | null | undefined)[]
// Still includes nullish in type!
After:
const arr = [1, null, 2, undefined, 3];
const filtered = arr.filter(Boolean);
// filtered: number[]
// Correctly narrows the type!
4. array.includes() Works With Readonly
Before:
const arr = ["a", "b", "c"] as const;
const input: string = getInput();
arr.includes(input);
// Error: Argument of type 'string' is not assignable to
// parameter of type '"a" | "b" | "c"'
After:
const arr = ["a", "b", "c"] as const;
const input: string = getInput();
arr.includes(input); // Works!
// Returns boolean, properly typed
5. Set.has() Works Ergonomically
Before:
const set = new Set(["a", "b", "c"]);
const input: string = getInput();
set.has(input);
// Error with strict types
After:
const set = new Set(["a", "b", "c"]);
const input: string = getInput();
set.has(input); // Works!
6. Map.has() Returns Type Guard
Before:
const map = new Map<string, number>();
const key = "foo";
if (map.has(key)) {
map.get(key); // number | undefined — still needs check!
}
After:
const map = new Map<string, number>();
const key = "foo";
if (map.has(key)) {
map.get(key); // number — properly narrowed!
}
Available Rules
Import all or pick specific ones:
// All rules
import "@total-typescript/ts-reset";
// Individual rules
import "@total-typescript/ts-reset/json-parse";
import "@total-typescript/ts-reset/fetch";
import "@total-typescript/ts-reset/filter-boolean";
import "@total-typescript/ts-reset/array-includes";
import "@total-typescript/ts-reset/set-has";
import "@total-typescript/ts-reset/map-has";
import "@total-typescript/ts-reset/array-index-of";
import "@total-typescript/ts-reset/is-array";
Best Practices
- Add to every TypeScript project — It's a devDependency, no runtime cost
- Use with Zod or similar —
unknownreturn types pair perfectly with schema validation - Import in a
.d.tsfile — Keeps types clean, no bundled code - Use specific imports if needed — Some teams prefer granular control
Why This Matters
TypeScript's any return types are a major source of runtime errors. By returning unknown, ts-reset forces you to validate data before using it, catching bugs at compile time instead of runtime.
// The ts-reset philosophy:
// "If I don't know the shape, I shouldn't pretend I do"
const data = JSON.parse(input);
// Without ts-reset: data.user.name (pray it exists)
// With ts-reset: validate first, then access safely