Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/molvqingtai/type-error-decorator
TypeError Decorator for JavaScript & TypeScript
https://github.com/molvqingtai/type-error-decorator
Last synced: 2 days ago
JSON representation
TypeError Decorator for JavaScript & TypeScript
- Host: GitHub
- URL: https://github.com/molvqingtai/type-error-decorator
- Owner: molvqingtai
- License: mit
- Created: 2021-11-02T15:31:58.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-03-06T14:03:57.000Z (almost 2 years ago)
- Last Synced: 2024-10-27T08:06:38.456Z (about 2 months ago)
- Language: TypeScript
- Size: 1.04 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# type-error-decorator
[![version](https://img.shields.io/github/v/release/molvqingtai/type-error-decorator)](https://www.npmjs.com/package/@resreq/type-error-decorator) [![workflow](https://github.com/molvqingtai/type-error-decorator/actions/workflows/ci.yml/badge.svg)](https://github.com/molvqingtai/type-error-decorator/actions) [![download](https://img.shields.io/npm/dt/@resreq/type-error-decorator)](https://www.npmjs.com/package/@resreq/type-error-decorator) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
TypeError Decorator for JavaScript & TypeScript
When we make a TypeScript-based library, the compiled .d.ts will only work for TypeScript users, and if we need to add type checking for Vanilla JS users as well, we need to write redundant type of judgements.
This project provides some decorators that make it easy to add type checking to class.
## Install
```shell
npm i @resreq/type-error-decorator
```**or**
```shell
yarn add @resreq/type-error-decorator
```## Documentation
### Get Started
Using decorators in class.
```js
import { TypeClass, TypeMethod, TypeParam } from '@resreq/type-error-decorator'@TypeClass
class Http {
options? = {}
constructor(@TypeParam('Object',false) options?: any) {
this.options = options
}@TypeMethod
get(@TypeParam('String') url: any, options?: any) {}@TypeMethod
post(@TypeParam('String|URL') url: any, @TypeParam('Object') options: any) {}
}
```Testing your class, with an incorrect parameter type, will throw a TypeError.
```js
new Http(1)
// TypeError: constructor arguments[0] must be Object
``````js
const http = new Http()
http.get(1)
// TypeError: get arguments[0] must be String
``````js
const http = new Http()
http.post(1, {})
// TypeError: post arguments[0] must be String or URL
``````js
const http = new Http()
http.post(new URL('https://www.example.com/'))
// TypeError: post arguments[1] is required
```The decorator uses [Symbol.toStringTag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) internally to compare types, so you can use all the built-in constructor names (Upper Camel Case) to define types.
For example:
```js
@TypeParam('BigInt|RegExp|Symbol|Request|Response|...')
```### Decorators
**TypeClass**
- **Arguments:** No
- **Usage:**
The check constructor must have @TypeClass added to the class.
**TypeMethod**
- **Arguments:** No
- **Usage:**
The check method must have @TypeMethod added to the method.
**TypeParam(type, required = true)**
- **Arguments:**
- `{string} type`
- `{Boolean?} required`- **Usage:**
Add @TypeParam to the parameters of the method to set the type of check.
## License
This project is licensed under the MIT License - see the [LICENSE](https://github.com/molvqingtai/type-error-decorator/blob/master/LICENSE) file for details