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

@putout/plugin-types

v5.1.0

Published

🐊Putout plugin adds ability to transform code related to types assertions

Downloads

26,807

Readme

@putout/plugin-types NPM version

🐊Putout plugin adds ability to help with transforming code related to types.

Install

npm i putout @putout/plugin-types -D

Rules

Config

{
    "rules": {
        "types/declare": "on",
        "types/convert-typeof-to-istype": "on",
        "types/remove-useless-conversion": "on",
        "types/remove-useless-constructor": "on",
        "types/remove-double-negations": "on",
        "types/remove-useless-typeof": "on",
        "types/apply-is-array": "on"
    }
}

declare

Based on @putout/operator-declare. Supported assertions:

  • isString;
  • isEmptyString;
  • isNumber;
  • isFn;
  • isBool;
  • isObject;
  • isUndefined;
  • isSymbol;
  • isNull;
  • isBigInt;
  • isArray;
  • isEmptyArray;

❌ Example of incorrect code

isString('hello');

✅ Example of correct code

const isString = (a) => typeof a === 'string';
isString('hello');

When you want to skip some declaration use dismiss:

{
    "rules": {
        "types/declare": ["on", {
            "dismiss": ["isString"]
        }]
    }
}

convert-typeof-to-is-type

The typeof operator returns a string indicating the type of the unevaluated operand.

(c) MDN

❌ Example of incorrect code

if (typeof a === 'boolean')
    return x;

✅ Example of correct code

const isBool = (a) => typeof a === 'boolean';

if (isBool(a))
    return x;

remove-useless-conversion

❌ Example of incorrect code

const a = !![1].includes(1);
const b = Boolean([1].includes(1));

✅ Example of correct code

const a = [1].includes(1);

remove-useless-constructor

Wrapper classes have surprising behaviour, such as new Boolean(false) evaluating to true.

(c) Google TypeScript Style Guide

🐊Putout plugin adds ability to remove useless constructor. Use with new/remove-useless.

❌ Example of incorrect code

const s = String('hello');
const b = Boolean(false);
const n = Number(5);

✅ Example of correct code

const s = 'hello';
const b = false;
const n = 5;

remove-double-negations

It is possible to use a couple of NOT operators (!!) in series to explicitly force the conversion of any value to the corresponding boolean primitive. The conversion is based on the "truthyness" or "falsyness" of the value.

The same conversion can be done through the Boolean function.

(c) MDN

❌ Example of incorrect code

if (!!a)
    console.log('hi');

✅ Example of correct code

if (a)
    console.log('hi');

remove-useless-typeof

The typeof operator returns a string indicating the type of the unevaluated operand.

(c) MDN

❌ Example of incorrect code

typeof typeof 'hello';

✅ Example of correct code

typeof 'hello';

apply-is-array

The Array.isArray() method determines whether the passed value is an Array. When checking for Array instance, Array.isArray() is preferred over instanceof because it works through iframes.

❌ Example of incorrect code

x instanceof Array;

✅ Example of correct code

const {isArray} = Array;
isArray(x);

In case of using inline option:

{
    "rules": {
        "types/apply-is-array": ["on", {
            "inline": true
        }]
    }
}

Array.isArray will be inlined:

Array.isArray(x);

License

MIT

Comparison

Linter | Rule | Fix --------|-------|------------| 🐊 Putout | types| ✅ ⏣ ESLint | no-implicit-coercion | ✅

License

MIT