目录

@typescript-eslint/no-inferrable-types

Disallow explicit type declarations for variables or parameters initialized to a number, string, or boolean.

TypeScript is able to infer the types of parameters, properties, and variables from their default or initial values. There is no need to use an explicit type annotation on one of those constructs initialized to a boolean, number, or string.

This rule applies only to .js and .ts files.

Rule Configuration

// code-linter.json5
{
  "rules": {
    "@typescript-eslint/no-inferrable-types": "error"
  }
}

Options

For details, see @typescript-eslint/no-inferrable-types.

Example of Correct Code

const num = 10;
export const a1 = 10n;
export const a2 = BigInt(num);
export const a3 = !num;
export const a4 = Boolean(null);
export const a5 = true;
export const a6 = null;

export class Foo {
  public prop = num;
}

export function fn(a = num, b = true): void {
  console.info(`${a}${b}`);
}

Example of Incorrect Code

const num: number = 10;
export const a1: bigint = 10n;
export const a2: bigint = BigInt(num);
export const a3: boolean = !num;
export const a4: boolean = Boolean(null);
export const a5: boolean = true;
export const a6: null = null;

export class Foo {
  public prop: number = num;
}

export function fn(a: number = num, b: boolean = true): void {
  console.info(`${a}${b}`);
}

Rule Set

plugin:@typescript-eslint/all

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