πPutout plugin adds ability to transform code related to types assertions
npm install @putout/plugin-types[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-types.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-types "npm"
πPutout plugin adds ability to help with transforming code related to types.
```
npm i putout @putout/plugin-types -D
- β
apply-is-array;
- β
convert-typeof-to-is-type;
- β
declare;
- β
remove-double-negations;
- β
remove-useless-conversion;
- β
remove-useless-constructor;
- β
remove-useless-typeof;
`json`
{
"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"
}
}
Based on @putout/operator-declare.
Supported assertions:
- isString;isEmptyString
- ;isNumber
- ;isNumberLike
- - checks if a can be convert to Number from String;isFn
- ;isBool
- ;isObject
- ;isUndefined
- ;isSymbol
- ;isNull
- ;isBigInt
- ;isArray
- ;isEmptyArray
- ;isError
- ;
`js`
isString('hello');
isNumber('a' - 5);
`js
const isString = (a) => typeof a === 'string';
const isNumber = (a) => !Number.isNaN(a) && typeof a === 'number';
isString('hello');
isNumber('a' - 5);
`
When you want to skip some declaration use dismiss:
`json`
{
"rules": {
"types/declare": ["on", {
"dismiss": ["isString"]
}]
}
}
> The typeof operator returns a string indicating the type of the unevaluated operand.
>
> (c) MDN
`js`
if (typeof a === 'boolean')
return x;
`js
const isBool = (a) => typeof a === 'boolean';
if (isBool(a))
return x;
`
`js`
const a = !![1].includes(1);
const b = Boolean([1].includes(1));
`js`
const a = [1].includes(1);
> 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.
`js`
const s = String('hello');
const b = Boolean(false);
const n = Number(5);
`js`
const s = 'hello';
const b = false;
const n = 5;
> 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.Boolean
>
> The same conversion can be done through the function.
>
> (c) MDN
`js`
if (!!a)
console.log('hi');
`js`
if (a)
console.log('hi');
> The typeof operator returns a string indicating the type of the unevaluated operand.
>
> (c) MDN
`js`
typeof typeof 'hello';
`js`
typeof 'hello';
> The Array.isArray() method determines whether the passed value is an Array.Array
> When checking for instance, Array.isArray() is preferred over instanceof because it works through iframes.
`js`
x instanceof Array;
`js`
const {isArray} = Array;
isArray(x);
In case of using inline option:
`json`
{
"rules": {
"types/apply-is-array": ["on", {
"inline": true
}]
}
}
Array.isArray will be inlined:
`js`
Array.isArray(x);
MIT
Linter | Rule | Fix
--------|-------|------------|
π Putout | types| β
β£ ESLint | no-implicit-coercion` | β
MIT