What is "no-string-literal" rule in tslint?
Asked Answered
D

1

21

The description found here is very short:

Disallows object access via string literals.

Is there any further documentation or example?

Diagnostic answered 18/4, 2016 at 13:37 Comment(0)
M
25

It means you can't access objects using the bracket notation. For example:

object["property"]

This way, a simple typo will throw an error and that's why it should be avoided. Instead, it is encouraged to use strongly-typed access.

Moorwort answered 18/4, 2016 at 13:40 Comment(4)
Why do I need such of rule?Diagnostic
agreed, I dislike this rule becose sometimes you have to use object literalsMckeon
The reason for this rule is because there is no need to use a string literal to access a property, other than cases like obj["prop-erty"] (which are allowed by this rule).Molecule
I do like this rule because it prevents accessing a private or protected property / method when we are not supposed to. I do have to disable this rule on occasion (mostly on unit tests) inside the file itself.Conveyancer

© 2022 - 2024 — McMap. All rights reserved.