目录

@typescript-eslint/prefer-nullish-coalescing

Enforce using the nullish coalescing operator (??) instead of logical operators.

Rule Configuration

// code-linter.json5
{
  "rules": {
    "@typescript-eslint/prefer-nullish-coalescing": "error"
  }
}

Options

For details, see @typescript-eslint/prefer-nullish-coalescing.

Example of Correct Code

function getText1(): string | undefined {
  return 'bar';
}

function getText2(): string | null {
  return 'bar';
}

const foo1: string | undefined = getText1();
export const v1 = foo1 ?? 'a string';

const foo2: string | null = getText2();
export const v2 = foo2 ?? 'a string';

Example of Incorrect Code

declare const a: string | null;
declare const b: string | null;

export const c = a || b;

Rule Set

plugin:@typescript-eslint/all

For details about how to configure Code Linter check rules, see Code Linter.