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.
npm install better-typescript-libVerified import paths — ran on the pinned version, not inferred.
Shows installation (npm, tsconfig, pnpm) and basic type improvements: JSON.parse returns JSONData, Object.keys returns keyof array, getElementById returns Element|null.
Use type guards or assertions to narrow JSONData to expected shape before property access.
Use explicit type assertions if you need string[]: Object.keys(obj) as string[]
Cast to HTMLElement if needed: document.getElementById('id') as HTMLElementUpgrade to TypeScript >=4.5.2 and better-typescript-lib v2.
Add `public-hoist-pattern[]=@typescript/*` to .npmrc.
Set `"libReplacement": true` in compilerOptions.
Add `public-hoist-pattern[]=@typescript/*` to .npmrc and reinstall.
Use a type guard or runtime validation: if (typeof data === 'object' && data !== null) { /* now data is JSONData object */ }If you need string[], cast: (Object.keys(obj) as string[]) or use a widening: String[] = Object.keys(obj) as any