{"id":18162,"library":"better-typescript-lib","title":"better-typescript-lib","description":"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.","status":"active","version":"2.12.0","language":"javascript","source_language":"en","source_url":"https://github.com/uhyo/better-typescript-lib","tags":["javascript","TypeScript","typescript"],"install":[{"cmd":"npm install better-typescript-lib","lang":"bash","label":"npm"},{"cmd":"yarn add better-typescript-lib","lang":"bash","label":"yarn"},{"cmd":"pnpm add better-typescript-lib","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency; the library patches TypeScript's built-in lib files. Must match TypeScript version range >=4.5.2.","package":"typescript","optional":false}],"imports":[{"note":"The package works automatically after installation; no explicit import required. It replaces TypeScript's lib declarations at the compiler level.","wrong":"import { betterTypescriptLib } from 'better-typescript-lib'","symbol":"better-typescript-lib","correct":"import {} from 'better-typescript-lib' // or just install, no import needed"},{"note":"With better-typescript-lib, JSON.parse returns JSONData (a recursive type) instead of any, forcing type narrowing before access.","wrong":"const data: any = JSON.parse('...')","symbol":"JSON.parse","correct":"const data: JSONData = JSON.parse('...')"},{"note":"better-typescript-lib changes Object.keys return type from string[] to (keyof T)[] in TypeScript >=4.6, which may break code expecting string[].","wrong":"const keys: string[] = Object.keys(obj) // use (keyof T)[] in TS 4.6+","symbol":"Object.keys","correct":"const keys: (keyof T)[] = Object.keys(obj)"}],"quickstart":{"code":"// Ensure you have TypeScript >=4.5.2\n// Install better-typescript-lib as dev dependency\nnpm install -D better-typescript-lib\n\n// For TypeScript 5.8+, add to tsconfig.json:\n{\n  \"compilerOptions\": {\n    \"libReplacement\": true\n  }\n}\n\n// For pnpm, add to .npmrc:\npublic-hoist-pattern[]=@typescript/*\n\n// No import needed. Example usage:\nconst parsed = JSON.parse('{\"foo\": 42}');\n// parsed: JSONData (not any)\n\nconst obj = { foo: 42, bar: 'hello' };\nconst keys = Object.keys(obj);\n// keys: (keyof typeof obj)[] = ('foo' | 'bar')[]\n\nconst el = document.getElementById('myId');\n// el: Element | null (not HTMLElement | null)\n","lang":"typescript","description":"Shows installation (npm, tsconfig, pnpm) and basic type improvements: JSON.parse returns JSONData, Object.keys returns keyof array, getElementById returns Element|null."},"warnings":[{"fix":"Use type guards or assertions to narrow JSONData to expected shape before property access.","message":"JSON.parse returns JSONData instead of any. Accessing properties without narrowing causes compile errors.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Use explicit type assertions if you need string[]: Object.keys(obj) as string[]","message":"Object.keys and Object.entries return keyof-typed arrays instead of string[]/Array<[string, T]> in TS 4.6+.","severity":"breaking","affected_versions":">=2.8.0"},{"fix":"Cast to HTMLElement if needed: document.getElementById('id') as HTMLElement","message":"document.getElementById returns Element | null instead of HTMLElement | null.","severity":"breaking","affected_versions":">=2.11.0"},{"fix":"Upgrade to TypeScript >=4.5.2 and better-typescript-lib v2.","message":"The v1 branch (for TS 4.4 and earlier) is no longer maintained.","severity":"deprecated","affected_versions":"<2.0.0"},{"fix":"Add `public-hoist-pattern[]=@typescript/*` to .npmrc.","message":"pnpm does not automatically hoist @typescript/* packages; require .npmrc config.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Set `\"libReplacement\": true` in compilerOptions.","message":"TypeScript 5.8+ requires `libReplacement: true` in tsconfig.json for better-typescript-lib to take effect (future default may change).","severity":"gotcha","affected_versions":">=2.11.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Add `public-hoist-pattern[]=@typescript/*` to .npmrc and reinstall.","cause":"With pnpm, @typescript/* packages are not hoisted to node_modules root.","error":"Cannot find module '@typescript/lib-es2015' or its corresponding type declarations."},{"fix":"Use a type guard or runtime validation: if (typeof data === 'object' && data !== null) { /* now data is JSONData object */ }","cause":"JSON.parse returns JSONData, which is a union type (string | number | boolean | null | JSONData[] | { [key: string]: JSONData }).","error":"Type 'JSONData' is not assignable to type 'MyInterface'."},{"fix":"If you need string[], cast: (Object.keys(obj) as string[]) or use a widening: String[] = Object.keys(obj) as any","cause":"Object.keys returns (keyof T)[] instead of string[], so array methods behave differently.","error":"Property 'foo' does not exist on type 'string[]'. Did you mean 'foo'?"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}