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?
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
!
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 theallow-type-parameters
option inprefer-array-literal
: