A type replacement for enhancing TypeScript built-in apis.
npm install @fenge/types




A type replacement for enhancing TypeScript built-in apis.
TypeScript supports replacing built-in definitions by installing a lib to node_modules, since v4.5. TypeScript built-in definitions have a large number of any, which are not type-safe enough.
This is a library that provides stricter type definitions for enhancing TypeScript built-in apis.
Here are all features and differences between the built-in definitions and this library.
Without this library:
- 🚨 JSON.parse returns any.
- 🚨 Array.isArray returns any[].
- 🚨 new Map() generates Map.
- 🚨 new Promise() can reject a non Error variable.
- 🚨 Promise.reject accepts any as a reason.
- 🚨 Promise.prototype.catch accepts (reason: any) => void | PromiseLike.
- 🚨 Promise.prototype.then accepts (reason: any) => void | PromiseLike for the second parameter.
With this library:
- 👍 JSON.parse returns unknown.
- 👍 Array.isArray returns unknown[].
- 👍 new Map() generates Map.
- 👍 new Promise() must reject an Error variable.
- 👍 Promise.reject accepts Error as a reason.
- 👍 Promise.prototype.catch accepts (reason: unknown) => void | PromiseLike.
- 👍 Promise.prototype.then accepts (reason: unknown) => void | PromiseLike for the second parameter.
Firstly, if you have install @types/node, make sure its version >= 18.0.0.
Then, add this library to devDependencies field in package.json file. You can replace the version of 0.4.0 with the expected version.
``json`
{
"devDependencies": {
"@typescript/lib-es2020": "npm:@fenge/types@0.4.0"
}
}
Finally, run npm install or yarn install or pnpm install.
After that, writing TypeScript code will be more type-safe. Example:
`tsfoo
const foo = JSON.parse('{"bar": 1}'); // The is unknown type now.``
console.log(foo.baz + 1); // error: 'foo' is of type 'unknown'.
MIT