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

eslint-plugin-typelint

v1.0.9

Published

ESlint rule for optional typing in JavaScript, based on JSDoc

Downloads

670

Readme

TypeLint

TypeLint is an ESlint plugin for static type checking in JavaScript, based on JSDoc.

Every application manages data, which is usually described in some way.

For example:

  • Swagger definitions
  • Redux store (folded reducers)
  • JSON based database schemas (MongoDB, RethinkDB, CouchDB etc)
  • GraphQL schemas

Mission of TypeLint is to provide complex type checking, based on already described data structures of your app.

Demo

See also Typelint example project

Installation

npm i eslint eslint-plugin-typelint

If you use eslint globally, you should install typelint globally as well.

Put typelint to eslint plugins array and add rule typelint/typelint

Supported types declarations

All types are compatible with JSDoc and Closure Compiler. See http://eslint.org/doctrine/demo/ for examples

  • @param {TypeName} varName Where TypeName is previously defined type. See type definition section below.
  • @param {[TypeName]} varName Array of type TypeName
  • @param {{a: String, b: {c: [Number]}}} Nested record types
  • @param {TypeName1|TypeName2} Union types

Type definitions

Redux state

Redux is a popular store for React apps.

It describes the state shape as a composition of reducers.

Dealing with a big and deeply nested store it's easy to make mistakes with access to a wrong property.

Typelint constructs the Redux schema, based on initial values of reducers, detecting types of end values.

To use it, just add the redux option to settings.typelint.models section of .eslintrc with path to your root reducer:

"settings": {
  "typelint": {
    "models": {
      "redux": {
        "reducerPath": "./src/client/redux/reducer.js"
      }
    },
    "useCache":  true
  }
}

After that you can use a new typelint type in JSDoc comments.

API/Swagger

TypeLint supports JSON Schema for describing data interfaces.

JSON Schema is advanced, popular and well documented format for describing JSON data.

For example if you use Swagger for API, you already have JSON Schema definitions, that you can use.

To bind your models to TypeLint, put the json option to settings.typelint.models section of your .eslintrc:

  "settings": {
    "typelint": {
      "models": {
        "json": {
          "dir": "./models",
          "exclude": ["wrong_dir"]
        },
      }
    }
  }

When mentioned "dir" option, models are expected as files, where each of them contains one definition. Name of the file is a name of the model. When "swagger" option is used instead of "dir", the particular path is expected as a Swagger schema with "definitions" section. See Swagger docs for more information.

Supported formats:

  • JSON
  • YAML
  • JS files as common.js modules (export object is a schema)

Additional features

Adapters

Adapters can be used for transforming schemas from one format to another. For example:

"json": {
  "dir": "./models",
  "exclude": ["wrong_dir"],
  "adapters": ["./node_modules/eslint-plugin-typelint/adapters/to-camel-case"]
},

There are two already existed adapters: to-camel-case and to-snake-case. They appropriately convert fields of schema. You can write your own adapters, using the same interface.

Autocomplete

If you use WebStorm/PHPStorm 2016, all TypeLint types are available for autocomplete. Just run in your working directory:

cp node_modules/eslint-plugin-typelint/jsonSchemas.xml .idea/

What it is and what it is not

TypeLint is a helper, but not a full-fledged typed system for js.

If you want to make your code 100% typed, please use any of existing static typed languages, which can be transpiled to JavaScript (TypeScript, Flow etc)

The purpose of TypeLint is to help developer avoid undefined errors, but optionally and staying all the speed and flexibility of pure JavaScript developement.

BTW TypeLint was written with help of TypeLint 😊️

All .eslinrc typelint settings options

  • models.json.dir - {String} path to your models dir. Every file is a separate model
  • models.json.swagger - {String} path to your swagger schema file
  • models.json.exclude - {Array} array of paths, that models finder should ignore while loading models
  • models.json.adapters - {Array} array of paths to modules, which exports functions :: yourSchema -> JSONSchema.
  • models.redux.reducerPath - {String} Path to the root Redux reducer
  • useCache - {Boolean} caches all models (will work faster, but changes in models will not affect). Default: false

Planned features

  • Handle passing typed variables throw
  • Detect when variable was reassigned
  • Improve caching and performance
  • Support GraphQL schemas
  • Adapters for Mongoose and Thinky
  • Other types of models bindings

Changelog

Article "Optional typing in JavaScript without transpilers"

Type features comparison: Haskell, TypeScript, Flow, JSDoc, JSON Schema

License

MIT.

Build Status Codewake