📝 Enforce consistent naming for boolean names.
💼🚫 This rule is enabled in the ✅ recommended config. This rule is disabled in the ☑️ unopinionated config.
🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
By default, this rule checks boolean variables, parameters, and functions. When checkProperties is enabled, it also checks object, class, and TypeScript property and method names.
Boolean names should start with a prefix that makes the boolean meaning clear.
Names that start with a boolean prefix should also refer to booleans or boolean-returning functions. Unknown values are ignored.
Configured wrapper bindings may use boolean prefixes when a configured property or method provides a boolean-like value. This applies only to variables and parameters that are not reassigned.
Reports for property and method names, and reports for non-boolean values using boolean prefixes, do not provide rename suggestions.
The default prefixes are:
isarehashavecanshouldwasweredidwillrequires
The plural prefixes (are, have, were) allow names for boolean collections, like areFilesValid.
The prefix must be a distinct word part. isReady, is_ready, and IS_READY are allowed, but island is not considered to have the is prefix.
React hook function bindings are checked after the required use prefix. For example, useIsReady is treated as a boolean hook name, while useReady is not. ignore patterns still match the original source name, like useReady.
React refs initialized with a boolean-like value may use boolean prefixes when the binding name ends in Ref or Reference, such as isMountedRef, hasConsentRef, or hasConsentReference. The suffix identifies the binding as a ref object. The binding must not be reassigned after initialization.
Direct Vue ref() calls with boolean-like values and computed() calls with boolean-returning functions may use boolean prefixes, such as isBranch or hasDepartment. The binding must not be reassigned after initialization.
This rule intentionally does not check destructuring bindings, imports, class names, or catch parameters.
TypeScript type annotation checks resolve local type aliases and callable interfaces, but not qualified or namespaced type references.
This rule is only automatically fixable when a non-global, non-exported, non-ambient variable binding can be safely renamed to the first enabled prefix without adding a collision suffix. Other safe rename candidates are still provided as editor suggestions.
// ❌
const completed = true;
// ✅
const isCompleted = true;// ❌
const hasName = 'Sindre';
// ✅
const name = 'Sindre';// ❌
function hasTitle() {
return 'Unicorn';
}
// ✅
function getTitle() {
return 'Unicorn';
}// ❌
const completed = progress === 100;
// ✅
const hasCompleted = progress === 100;// ❌
const completed = Boolean(value);
// ✅
const isCompleted = Boolean(value);// ❌
function download(showProgress = false) {}
// ✅
function download(shouldShowProgress = false) {}// ❌
const completed: boolean = true;
// ✅
const isCompleted: boolean = true;// ❌
function completed() {
return true;
}
// ✅
function isCompleted() {
return true;
}// ❌
function download(showProgress: boolean) {}
// ✅
function download(shouldShowProgress: boolean) {}// ✅
// Properties are ignored unless `checkProperties` is enabled.
const task = {
completed: progress === 100,
};Type: boolean
Default: false
Check object, class, and TypeScript property and method names.
'unicorn/consistent-boolean-name': [
'error',
{
checkProperties: true,
},
]With checkProperties: true, this would fail:
const task = {
completed: true,
};interface Task {
completed: boolean;
}And this would pass:
const task = {
isCompleted: true,
};interface Task {
isCompleted: boolean;
canComplete(): boolean;
}Type: Record<string, boolean>
Default:
{
is: true,
are: true,
has: true,
have: true,
can: true,
should: true,
was: true,
were: true,
did: true,
will: true,
requires: true,
}The prefixes option is merged with the defaults. Set a prefix to true to allow it for boolean names and reserve it for boolean-like values. Set a prefix to false to disable it in both directions.
'unicorn/consistent-boolean-name': [
'error',
{
prefixes: {
needs: true,
did: false,
},
},
]With the above config, this would pass:
const needsUpdate = true;And this would fail:
const didUpdate = true;Type: Record<string, string>
Default: {}
Map unqualified TypeScript wrapper type names to the property or method that provides a boolean-like value. Same-named types share configuration. Derived types, intersections, and constrained type parameters are supported. Members may provide boolean, Promise<boolean>, or PromiseLike<boolean>; nullable results are accepted. Requires TypeScript type information.
'unicorn/consistent-boolean-name': [
'error',
{
wrappers: {
StorageItem: 'get',
},
},
]With the above configuration, this would pass:
interface StorageItem<Base, Return = Base | undefined> {
get(): Promise<Return>;
}
declare const isUnicorn: StorageItem<unknown, boolean>;But this would still fail because get() returns a string:
interface StorageItem<Base, Return = Base | undefined> {
get(): Promise<Return>;
}
declare const isUnicorn: StorageItem<unknown, string>;Type: Array<string | RegExp>
Default: []
Names matching any of these patterns are not checked. Strings are treated as regular expressions, so they match anywhere in the name unless anchored with ^ and $.
'unicorn/consistent-boolean-name': [
'error',
{
ignore: [
'value',
'^completed$',
],
},
]With the above config, these would pass:
const value = true;
const completed = true;