npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@resreq/type-error-decorator

v1.0.6

Published

TypeError Decorator for JavaScript & TypeScript

Downloads

3

Readme

type-error-decorator

version workflow download JavaScript Style Guide

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

npm i @resreq/type-error-decorator

or

yarn add @resreq/type-error-decorator

Documentation

Get Started

Using decorators in class.

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.

new Http(1)
// TypeError: constructor arguments[0] must be Object
const http = new Http()
http.get(1)
// TypeError: get arguments[0] must be String
const http = new Http()
http.post(1, {})
// TypeError: post arguments[0] must be String or URL
const http = new Http()
http.post(new URL('https://www.example.com/'))
// TypeError: post arguments[1] is required

The decorator uses Symbol.toStringTag internally to compare types, so you can use all the built-in constructor names (Upper Camel Case) to define types.

For example:

@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 file for details