Tslint: array-type vs prefer-array-literal

Created on 23 Jun 2017  ·  3Comments  ·  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.4.3
  • __TypeScript version__: 2.3.4
  • __Running TSLint via__: WebStorm

TypeScript code being linted

I want values to be a mixed array of either an object or a number.

// Violates 'array-type' rule
const values: (object | number)[] = []
// Violates 'prefer-array-literal' rule
const values: Array<object | number> = []

with tslint.json configuration:

{
  "extends": [
    "tslint:latest",
    "tslint-eslint-rules",
    "tslint-config-airbnb"
  ]
}

How can I write my code so I won't violate both rules? Are those two conflicting with each other and I have to give up one rule?

External

Most helpful comment

For everyone getting here from Google search, this can also be fixed without changing the array-type rule and reformatting array usages. Enable the allow-type-parameters option in prefer-array-literal:

{
  "rules": {
    "prefer-array-literal": [true, { "allow-type-parameters": true }]
  }
}

All 3 comments

It seems like prefer-array-literal just isn't compatible with the default config for the array-type rule. That rule isn't maintained by tslint, it's part of tslint-microsoft-contrib. You could set the "array" option on array-type to get them to work together. You might want to file an issue on tslint-config-airbnb about this.

For everyone getting here from Google search, this can also be fixed without changing the array-type rule and reformatting array usages. Enable the allow-type-parameters option in prefer-array-literal:

{
  "rules": {
    "prefer-array-literal": [true, { "allow-type-parameters": true }]
  }
}

Closing as external. Feel free to post an issue on tslint-microsoft-contrib!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CSchulz picture CSchulz  ·  3Comments

jacob-robertson picture jacob-robertson  ·  3Comments

jamesarosen picture jamesarosen  ·  3Comments

SwintDC picture SwintDC  ·  3Comments

avanderhoorn picture avanderhoorn  ·  3Comments