Skip to content
← Back to rules

typescript/array-type 风格

An auto-fix is available for this rule.

它的作用

强制一致地使用 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

type: "array" | "array-simple" | "generic"

default: "array"

用于可变情况下的期望数组类型。

readonly

type: "array" | "array-simple" | "generic"

default: null

用于只读情况下的期望数组类型。如果省略,则使用 default 的值。

如何使用

要通过配置文件或 CLI 启用 此规则,可以使用:

json
{
  "rules": {
    "typescript/array-type": "error"
  }
}
bash
oxlint --deny typescript/array-type

参考资料