Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Latest commit

 

History

History
83 lines (55 loc) · 1.51 KB

no-inferrable-types.md

File metadata and controls

83 lines (55 loc) · 1.51 KB

Disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean. (no-inferrable-types)

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

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 has an options object:

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

Default

When none of the options are truthy, the following patterns are valid:

const foo = 5;
const bar = true;
const baz = "str";

class Foo {
    prop = 5;
}

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

function fn(a: number, b: boolean, c: string) {}

The following are invalid:

const foo: number = 5;
const bar: boolean = true;
const baz: string = "str";

class Foo {
    prop: number = 5;
}

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

ignoreProperties

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

class Foo {
    prop: number = 5;
}

ignoreParameters

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

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

When Not To Use It

If you do not want to enforce inferred types.

Further Reading

TypeScript Inference

Compatibility

TSLint: no-inferrable-types