🐊Putout plugin adds ability to help with transforming code related to types.
npm i putout @putout/plugin-types -D
{
"rules": {
"types/declare": "on",
"types/convert-typeof-to-istype": "on",
"types/remove-useless-conversion": "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
;isFn
;isBool
;isObject
;isUndefined
;isSymbol
;isNull
;
isString('hello');
const isString = (a) => typeof a === 'string';
isString('hello');
When you want to skip some declaration use dismiss
:
{
"rules": {
"types/declare": ["on", {
"dismiss": [
"isString"
]
}]
}
}
The
typeof
operator returns a string indicating the type of the unevaluated operand.(c) MDN
if (typeof a === 'boolean')
return x;
const isBool = (a) => typeof a === 'boolean';
if (isBool(a))
return x;
const a = !![1].includes(1);
const b = Boolean([1].includes(1));
const a = [1].includes(1);
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
if (!!a) {
console.log('hi');
}
if (a) {
console.log('hi');
}
The
typeof
operator returns a string indicating the type of the unevaluated operand.(c) MDN
typeof typeof 'hello';
typeof 'hello';
The
Array.isArray()
method determines whether the passed value is anArray
. When checking forArray
instance,Array.isArray()
is preferred overinstanceof
because it works throughiframes
.
x instanceof Array;
const {isArray} = Array;
isArray(x);
In case of using inline
option:
{
"rules": {
"apply-is-array": ["on", {
"inline": true
}]
}
}
Array.isArray
will be inlined:
Array.isArray(x);
MIT
Linter | Rule | Fix |
---|---|---|
🐊 Putout | types |
✅ |
⏣ ESLint | no-implicit-coercion |
✅ |
MIT