typescript/array-type スタイル
何をするか
配列に対して、T[] または Array<T> のどちらかを一貫して使用することを要請します。
なぜこれは良くないのか
Array 型を直接使うことは、慣習的に適切ではありません。代わりに、配列型として T[] または Array<T> を使用してください。
例
このルールに違反する 誤り なコードの例:
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"];このルールに従った 正しい コードの例:
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"];設定
このルールは、以下のプロパティを持つ設定オブジェクトを受け入れます。
default
型: "array" | "array-simple" | "generic"
デフォルト: "array"
変更可能な状態で期待される配列型。
readonly
型: "array" | "array-simple" | "generic"
デフォルト: null
読み取り専用の状態で期待される配列型。省略された場合、default の値が使用されます。
使用方法
このルールを有効にするには、設定ファイルまたは CLI で次のように使用できます:
json
{
"rules": {
"typescript/array-type": "error"
}
}bash
oxlint --deny typescript/array-type