better-typescript-lib
raw JSON → 2.12.0 verified Sat Apr 25 auth: no javascript
An alternative TypeScript standard library that replaces TypeScript's built-in type definitions with safer, more precise types. Version 2.12.0 supports TypeScript 4.5+ (up to 5.8). It fixes many `any` usages in the standard library (e.g., JSON.parse returns `JSONData` instead of `any`, Object.keys returns `(keyof T)[]` instead of `string[]`). Aimed at new projects or codebases willing to accept breaking changes for improved type safety. No configuration needed for npm/yarn (auto-detected); pnpm requires public hoist pattern. The package replaces `@typescript/lib-*` packages via npm's scoped package mapping.
Common errors
error Cannot find module '@typescript/lib-es2015' or its corresponding type declarations. ↓
cause With pnpm, @typescript/* packages are not hoisted to node_modules root.
fix
Add
public-hoist-pattern[]=@typescript/* to .npmrc and reinstall. error Type 'JSONData' is not assignable to type 'MyInterface'. ↓
cause JSON.parse returns JSONData, which is a union type (string | number | boolean | null | JSONData[] | { [key: string]: JSONData }).
fix
Use a type guard or runtime validation: if (typeof data === 'object' && data !== null) { /* now data is JSONData object */ }
error Property 'foo' does not exist on type 'string[]'. Did you mean 'foo'? ↓
cause Object.keys returns (keyof T)[] instead of string[], so array methods behave differently.
fix
If you need string[], cast: (Object.keys(obj) as string[]) or use a widening: String[] = Object.keys(obj) as any
Warnings
breaking JSON.parse returns JSONData instead of any. Accessing properties without narrowing causes compile errors. ↓
fix Use type guards or assertions to narrow JSONData to expected shape before property access.
breaking Object.keys and Object.entries return keyof-typed arrays instead of string[]/Array<[string, T]> in TS 4.6+. ↓
fix Use explicit type assertions if you need string[]: Object.keys(obj) as string[]
breaking document.getElementById returns Element | null instead of HTMLElement | null. ↓
fix Cast to HTMLElement if needed: document.getElementById('id') as HTMLElement
deprecated The v1 branch (for TS 4.4 and earlier) is no longer maintained. ↓
fix Upgrade to TypeScript >=4.5.2 and better-typescript-lib v2.
gotcha pnpm does not automatically hoist @typescript/* packages; require .npmrc config. ↓
fix Add `public-hoist-pattern[]=@typescript/*` to .npmrc.
gotcha TypeScript 5.8+ requires `libReplacement: true` in tsconfig.json for better-typescript-lib to take effect (future default may change). ↓
fix Set `"libReplacement": true` in compilerOptions.
Install
npm install better-typescript-lib yarn add better-typescript-lib pnpm add better-typescript-lib Imports
- better-typescript-lib wrong
import { betterTypescriptLib } from 'better-typescript-lib'correctimport {} from 'better-typescript-lib' // or just install, no import needed - JSON.parse wrong
const data: any = JSON.parse('...')correctconst data: JSONData = JSON.parse('...') - Object.keys wrong
const keys: string[] = Object.keys(obj) // use (keyof T)[] in TS 4.6+correctconst keys: (keyof T)[] = Object.keys(obj)
Quickstart
// Ensure you have TypeScript >=4.5.2
// Install better-typescript-lib as dev dependency
npm install -D better-typescript-lib
// For TypeScript 5.8+, add to tsconfig.json:
{
"compilerOptions": {
"libReplacement": true
}
}
// For pnpm, add to .npmrc:
public-hoist-pattern[]=@typescript/*
// No import needed. Example usage:
const parsed = JSON.parse('{"foo": 42}');
// parsed: JSONData (not any)
const obj = { foo: 42, bar: 'hello' };
const keys = Object.keys(obj);
// keys: (keyof typeof obj)[] = ('foo' | 'bar')[]
const el = document.getElementById('myId');
// el: Element | null (not HTMLElement | null)