Tslint - type trivially inferred - Why is it bad practice to include the type here?

AngularTypescriptVisual Studio-Code

Angular Problem Overview


In VSCode the linter , tslint, complains when I add the following code, with the type:

serverId: number = 10;

And gives the following message:

> [tslint] Type number trivially inferred from a number literal, remove > type annotation (no-inferrable-types)

When I remove the type 'number', the message goes away.

Why is it bad practice to include the type information here?

Angular Solutions


Solution 1 - Angular

It is not a bad practice, but serverId: number = 10 is redundant, because number type is inferred when a property is assigned. This is what TSLint no-inferrable-types warns about:

> Explicit types where they can be easily inferred by the compiler make code more verbose.

Unless there is a chance that serverId property may be initially undefined but be defined later (for instance in constructor function), number can be safely omitted.

This approach works best with noImplicitAny option because this way there are no chances that a type will be omitted by mistake because it wasn't inferred.

Solution 2 - Angular

As was mentioned above, it's technically redundant and can be considered clutter. Personally I don't care for this opinion and prefer to have both the type and the value for a variety of specific minor workflow reasons and I don't consider it to be the level of clutter warranting a rule. If you want to disable it, here's how.

  • open tslint.json
  • find the "no-inferrable-types" attribute
  • add ignore-properties to its array

relevant tslint docs https://palantir.github.io/tslint/rules/no-inferrable-types/

Solution 3 - Angular

This error is due to your configuration in tslint.json file.

Either just initialize your variable as

serverId = 10;

or

serverId: number;

or just set your configuration for the no-inferrable-types in your tslint.json file as

no-inferrable-types: false

Solution 4 - Angular

If you came here looking for an eslint solution because tslint is being deprecated, add this rule to your .eslintrc.js file:

module.exports = {
  ...m
  rules: {
    ...,
    "@typescript-eslint/no-inferrable-types": "off",
    ...
  },
};

Solution 5 - Angular

It is unnecessary, it does not provide any new information. It is basically a comment saying "10 is a number".

Solution 6 - Angular

This might be weird coming for this now, but I was getting the similar error and couldn't find the "no-inferrable-types" attribute in the tslint.json file of my angular app. I don't know why it didn't generate that in the first place but I had to insert it here

"rules": {
    **"no-inferrable-types": false,**
    "directive-selector": [
      true,
      "attribute",
      "app",
      "camelCase"
    ],

And then it worked like a charm!

P.S. This is for someone who might be wandering with the same issue as I did, or I might be wrong because no one in any of the solutions mentioned that this has to be added from our end in the json file.

Solution 7 - Angular

It could be seen as noise. It's more important to type the non trivial parts

Solution 8 - Angular

in tslint.json file add or complete this rule:

"no-inferrable-types": [
  true,
  "ignore-params",
  "ignore-properties"
]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTony ScialoView Question on Stackoverflow
Solution 1 - AngularEstus FlaskView Answer on Stackoverflow
Solution 2 - AngularAlex SperaView Answer on Stackoverflow
Solution 3 - AngularPardeep JainView Answer on Stackoverflow
Solution 4 - AngularPaul Razvan BergView Answer on Stackoverflow
Solution 5 - AngularSulthanView Answer on Stackoverflow
Solution 6 - AngularAkshay KumarView Answer on Stackoverflow
Solution 7 - AngularJulianView Answer on Stackoverflow
Solution 8 - AngularMehdi RoostaeianView Answer on Stackoverflow