typescript/array-type Style
What it does
Require consistently using either T[]
or Array<T>
for arrays.
Why is this bad?
Using the Array
type directly is not idiomatic. Instead, use the array type T[]
or Array<T>
.
Examples
Examples of incorrect code for this rule:
typescript
/*oxlint array-type: ["error", { "default": "array" }] */
const arr: Array<number> = new Array<number>();
typescript
/*oxlint array-type: ["error", { "default": "generic" }] */
const arr: number[] = new Array<number>();
typescript
/*oxlint array-type: ["error", { "default": "array-simple" }] */
const a: (string | number)[] = ["a", "b"];
const b: { prop: string }[] = [{ prop: "a" }];
const c: Array<MyType> = ["a", "b"];
const d: Array<string> = ["a", "b"];
Examples of correct code for this rule:
typescript
/*oxlint array-type: ["error", { "default": "array" }] */
const arr: number[] = new Array<number>();
typescript
/*oxlint array-type: ["error", { "default": "generic" }] */
const arr: Array<number> = new Array<number>();
typescript
/*oxlint array-type: ["error", { "default": "array-simple" }] */
const a: Array<string | number> = ["a", "b"];
const b: Array<{ prop: string }> = [{ prop: "a" }];
const c: string[] = ["a", "b"];
const d: MyType[] = ["a", "b"];
Options
json
{
"typescript/array-type": ["error", { "default": "array", "readonly": "array" }]
}
default
: The array type expected for mutable cases.readonly
: The array type expected for readonly cases. If omitted, the value fordefault
will be used.
Both default
and readonly
can be one of:
"array"
"generic"
"array-simple"
The default config will enforce that all mutable and readonly arrays use the 'array' syntax.
How to use
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny typescript/array-type
json
{
"rules": {
"typescript/array-type": "error"
}
}