TSLint:object access via string literals is disallowedtslint(no-string-literal)

Today, when adding attributes to objects, tslint reported an error: object access through string text is not allowed (no string text)

const init = {
  id: 'aaa',
  name: 'bbb'
};
init['sex'] = 'ccc';

There are three solutions.

1: It is not allowed to pass strings. You can use variables

const name = 'sex';
const init = {
  id: 'aaa',
  name: 'bbb'
};
init[name] = 'ccc';

2: Let this line of code disable the tslint rule

const init = {
  id: 'aaa',
  name: 'bbb'
};
// tslint:disable-next-line:no-string-literal
init['sex'] = 'ccc';

3: Change tslint.json , disable this rule completely, add the following

"no-string-literal": false,

 

Read More: