@putout/plugin-types
v10.6.0
Published
🐊Putout plugin adds ability to transform code related to types assertions
Maintainers
Readme
@putout/plugin-types 
🐊Putout plugin adds ability to help with transforming code related to types.
Install
npm i putout @putout/plugin-types -DRules
- ✅ apply-boolean;
- ✅ apply-number;
- ✅ apply-is-array;
- ✅ apply-is-nan;
- ✅ convert-typeof-to-is-type;
- ✅ declare;
- ✅ remove-double-negations;
- ✅ remove-useless-conversion;
- ✅ remove-useless-constructor;
- ✅ remove-useless-typeof;
Config
{
"rules": {
"types/apply-boolean": "on",
"types/apply-number": "on",
"types/apply-is-array": "on",
"types/apply-is-nan": "on",
"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"
}
}apply-boolean
The
Boolean()constructor createsBooleanobjects. When called as a function, it returns primitive values of typeBoolean.(c) MDN
Checkout in 🐊Putout Editor.
❌ Example of incorrect code
!!(a || b);✅ Example of correct code
Boolean(a || b);apply-number
The
Numberconstructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using theNumber()function(c) MDN
Checkout in 🐊Putout Editor.
❌ Example of incorrect code
const a = {
BooleanLiteral(node) {
return {
value: node.value ? 1 : 0,
};
},
};✅ Example of correct code
const a = {
BooleanLiteral(node) {
return {
value: Number(node.value),
};
},
};apply-is-array
The
Array.isArray()method determines whether the passed value is anArray. When checking forArrayinstance,Array.isArray()is preferred overinstanceofbecause it works throughiframes.(c) MDN
❌ 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);apply-is-nan
The
NaNglobal property is a value representing Not-A-Number:NaN !== NaN.(c) MDN
Checkout in 🐊Putout Editor
❌ Example of incorrect code
if (a === NaN)
console.log();✅ Example of correct code
if (Number.isNaN(a))
console.log();declare
Based on @putout/operator-declare.
Supported assertions:
isString;isSet;isEmptyString;isNumber;isNumberLike- checks ifacan be convert toNumberfromString;isFn;isBool;isObject;isUndefined;isSymbol;isNull;isBigInt;isArray;isEmptyArray;isError;isRegExp;
❌ Example of incorrect code
isString('hello');
isNumber('a' - 5);✅ Example of correct code
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:
{
"rules": {
"types/declare": ["on", {
"dismiss": ["isString"]
}]
}
}convert-typeof-to-is-type
The
typeofoperator 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 totrue.
🐊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
Booleanfunction.(c) MDN
❌ Example of incorrect code
if (!!a)
console.log('hi');✅ Example of correct code
if (a)
console.log('hi');remove-useless-typeof
The
typeofoperator 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';Comparison
| Linter | Rule | Fix |
|---------------|------|-----|
| 🐊 Putout | types | ✅ |
| ⏣ ESLint | no-implicit-coercion | ✅ |
License
MIT
