0.4.0 • Published 5 months ago

@fenge/types v0.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@fenge/types

npm.io npm.io npm.io npm.io PRs Welcome

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.parse returns any.
  • 🚨 Array.isArray returns any[].
  • 🚨 new Map() generates Map<any, any>.
  • 🚨 new Promise() can reject a non Error variable.
  • 🚨 Promise.reject accepts any as a reason.
  • 🚨 Promise.prototype.catch accepts (reason: any) => void | PromiseLike<void>.
  • 🚨 Promise.prototype.then accepts (reason: any) => void | PromiseLike<void> for the second parameter.

With this library:

  • 👍 JSON.parse returns unknown.
  • 👍 Array.isArray returns unknown[].
  • 👍 new Map() generates Map<unknown, unknown>.
  • 👍 new Promise() must reject an Error variable.
  • 👍 Promise.reject accepts Error as a reason.
  • 👍 Promise.prototype.catch accepts (reason: unknown) => void | PromiseLike<void>.
  • 👍 Promise.prototype.then accepts (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

0.3.0

7 months ago

0.2.0

11 months ago

0.2.0-beta.2

11 months ago

0.2.0-beta.1

11 months ago

0.2.0-beta.0

11 months ago

0.4.0

5 months ago

0.1.0

1 year ago

0.1.0-beta.0

1 year ago