Registry / type-stubs / better-typescript-lib

better-typescript-lib

JSON →
library2.12.0jsnpmunverified

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-lib
INSTALL
IMPORT
SIG · BETTER-TYPESCRIPT-
B
better-typescript-lib
type-stubsjavascriptv2.12.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

better-typescript-lib
import {} from 'better-typescript-lib' // or just install, no import needed
import { betterTypescriptLib } from 'better-typescript-lib'
The package works automatically after installation; no explicit import required. It replaces TypeScript's lib declarations at the compiler level.
JSON.parse
const data: JSONData = JSON.parse('...')
const data: any = JSON.parse('...')
With better-typescript-lib, JSON.parse returns JSONData (a recursive type) instead of any, forcing type narrowing before access.
Object.keys
const keys: (keyof T)[] = Object.keys(obj)
const keys: string[] = Object.keys(obj) // use (keyof T)[] in TS 4.6+
better-typescript-lib changes Object.keys return type from string[] to (keyof T)[] in TypeScript >=4.6, which may break code expecting string[].

Shows installation (npm, tsconfig, pnpm) and basic type improvements: JSON.parse returns JSONData, Object.keys returns keyof array, getElementById returns Element|null.

// 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)
Debug
Known issues
breakingJSON.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.
affects: >=1.0.0
breakingObject.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[]
affects: >=2.8.0
breakingdocument.getElementById returns Element | null instead of HTMLElement | null.
fix
Cast to HTMLElement if needed: document.getElementById('id') as HTMLElement
affects: >=2.11.0
deprecatedThe v1 branch (for TS 4.4 and earlier) is no longer maintained.
fix
Upgrade to TypeScript >=4.5.2 and better-typescript-lib v2.
affects: <2.0.0
gotchapnpm does not automatically hoist @typescript/* packages; require .npmrc config.
fix
Add `public-hoist-pattern[]=@typescript/*` to .npmrc.
affects: >=2.0.0
gotchaTypeScript 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.
affects: >=2.11.0
Errors
Common errors & fixes
Cannot find module '@typescript/lib-es2015' or its corresponding type declarations.
With pnpm, @typescript/* packages are not hoisted to node_modules root.
fix
Add `public-hoist-pattern[]=@typescript/*` to .npmrc and reinstall.
Type 'JSONData' is not assignable to type 'MyInterface'.
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 */ }
Property 'foo' does not exist on type 'string[]'. Did you mean 'foo'?
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
Upgrade
Version history
2.12.0latest on npm
Audit
Dependencies
typescriptrequiredPeer dependency; the library patches TypeScript's built-in lib files. Must match TypeScript version range >=4.5.2.
Agent activity
63 hits · last 30 days
node
52
OpenAI (training)
1
Resources