Skip to main content

no-inferrable-types

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

Explicit types where they can be easily inferred may add unnecessary verbosity.

Attributes

  • Included in configs
    • ✅ Recommended
    • 🔒 Strict
  • Fixable
    • 🔧 Automated Fixer
    • 💡 Suggestion Fixer
  • 💭 Requires type information

Rule Details

This rule disallows explicit type declarations on parameters, variables and properties where the type can be easily inferred from its value.

Options

This rule accepts the following options:

interface Options {
ignoreParameters?: boolean;
ignoreProperties?: boolean;
}

Default

The default options are:

{
"ignoreParameters": false,
"ignoreProperties": false
}

With these options, the following patterns are:

const a: bigint = 10n;
const a: bigint = -10n;
const a: bigint = BigInt(10);
const a: bigint = -BigInt(10);
const a: boolean = false;
const a: boolean = true;
const a: boolean = Boolean(null);
const a: boolean = !0;
const a: number = 10;
const a: number = +10;
const a: number = -10;
const a: number = Number('1');
const a: number = +Number('1');
const a: number = -Number('1');
const a: number = Infinity;
const a: number = +Infinity;
const a: number = -Infinity;
const a: number = NaN;
const a: number = +NaN;
const a: number = -NaN;
const a: null = null;
const a: RegExp = /a/;
const a: RegExp = RegExp('a');
const a: RegExp = new RegExp('a');
const a: string = 'str';
const a: string = `str`;
const a: string = String(1);
const a: symbol = Symbol('a');
const a: undefined = undefined;
const a: undefined = void someValue;

class Foo {
prop: number = 5;
}

function fn(a: number = 5, b: boolean = true) {}

ignoreParameters

When set to true, the following pattern is considered valid:

function foo(a: number = 5, b: boolean = true) {
// ...
}

ignoreProperties

When set to true, the following pattern is considered valid:

class Foo {
prop: number = 5;
}

When Not To Use It

If you do not want to enforce inferred types.

Further Reading

TypeScript Inference