Issue
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?
Solution
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.
Answered By - Estus Flask
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.