@fenge/types v0.4.0
@fenge/types
A type replacement for enhancing TypeScript built-in apis.
What is it
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.
Features
Here are all features and differences between the built-in definitions and this library.
Without this library:
- 🚨
JSON.parsereturnsany. - 🚨
Array.isArrayreturnsany[]. - 🚨
new Map()generatesMap<any, any>. - 🚨
new Promise()can reject a nonErrorvariable. - 🚨
Promise.rejectacceptsanyas a reason. - 🚨
Promise.prototype.catchaccepts(reason: any) => void | PromiseLike<void>. - 🚨
Promise.prototype.thenaccepts(reason: any) => void | PromiseLike<void>for the second parameter.
With this library:
- 👍
JSON.parsereturnsunknown. - 👍
Array.isArrayreturnsunknown[]. - 👍
new Map()generatesMap<unknown, unknown>. - 👍
new Promise()must reject anErrorvariable. - 👍
Promise.rejectacceptsErroras a reason. - 👍
Promise.prototype.catchaccepts(reason: unknown) => void | PromiseLike<void>. - 👍
Promise.prototype.thenaccepts(reason: unknown) => void | PromiseLike<void>for the second parameter.
Usage
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.
{
"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:
const foo = JSON.parse('{"bar": 1}'); // The `foo` is `unknown` type now.
console.log(foo.baz + 1); // error: 'foo' is of type 'unknown'.License
MIT
7 months ago
11 months ago
11 months ago
11 months ago
11 months ago
5 months ago
1 year ago
1 year ago