sonarjs/prefer-type-guard
eslint-plugin-sonarjs
SuggestionRecommended
eslint.config.js
export default [
{
rules: {
"sonarjs/prefer-type-guard": "error",
},
},
];function isSomething(x: BaseType) : boolean { // Noncompliant
return (<Something>x).foo !== undefined;
}
if (isSomething(v)) {
(<Something>v).foo();
}function isSomething(x: BaseType) : x is Something {
return (<Something>x).foo !== undefined;
}
if (isSomething(v)) {
v.foo();
}Examples from the ESLint documentation.
sonarjs/prefer-type-guard
eslint-plugin-sonarjs
export default [
{
rules: {
"sonarjs/prefer-type-guard": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
The most specific assertion should be used
Debugging features should not be enabled in production
Return of boolean expressions should not be wrapped into an "if-then-else" statement
Pseudorandom number generators (PRNGs) should not be used in security contexts
"RegExp.exec()" should be preferred over "String.match()"
Public "static" fields should be read-only