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 (with default configuration):
const arr: Array<number> = new Array<number>();
const readonlyArr: ReadonlyArray<number> = [1, 2, 3];Examples of correct code for this rule (with default configuration):
const arr: number[] = new Array<number>();
const readonlyArr: readonly number[] = [1, 2, 3];Configuration
This rule accepts a configuration object with the following properties:
default
type: "array" | "array-simple" | "generic"
default: "array"
The array type expected for mutable cases.
"array"
Enforce using T[] for all array types.
Example of incorrect code for this option:
const arr: Array<number> = new Array<number>();Example of correct code for this option:
const arr: number[] = new Array<number>();"array-simple"
Enforce using T[] for simple types, and Array<T> for complex types.
Example of incorrect code for this option:
const a: (string | number)[] = ["a", "b"];
const b: { prop: string }[] = [{ prop: "a" }];
const c: Array<MyType> = ["a", "b"];
const d: Array<string> = ["a", "b"];Example of correct code for this option:
const a: Array<string | number> = ["a", "b"];
const b: Array<{ prop: string }> = [{ prop: "a" }];
const c: string[] = ["a", "b"];
const d: MyType[] = ["a", "b"];"generic"
Enforce using Array<T> for all array types.
Example of incorrect code for this option:
const arr: number[] = new Array<number>();Example of correct code for this option:
const arr: Array<number> = new Array<number>();readonly
type: "array" | "array-simple" | "generic"
default: null
The array type expected for readonly cases. If omitted, the value for default will be used.
How to use
To enable this rule using the config file or in the CLI, you can use:
{
"rules": {
"typescript/array-type": "error"
}
}import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/array-type": "error",
},
});oxlint --deny typescript/array-type