typescript/consistent-indexed-object-style Style
它的作用
在 Record 类型和索引签名类型之间进行选择时要求保持一致。
这两种类型是等价的,该规则会强制在两种风格中保持一致,二者择一:
ts
type Foo = Record<string, unknown>;
type Foo = {
[key: string]: unknown;
};为什么这不好?
索引对象类型风格不一致会影响项目的可读性。
示例
对于此规则,使用 consistent-indexed-object-style: ["error", "record"](默认值)时,错误 的代码示例如下:
ts
interface Foo {
[key: string]: unknown;
}
type Foo = {
[key: string]: unknown;
};对于此规则,使用 consistent-indexed-object-style: ["error", "record"](默认值)时,正确 的代码示例如下:
ts
type Foo = Record<string, unknown>;对于此规则,使用 consistent-indexed-object-style: ["error", "index-signature"] 时,错误 的代码示例如下:
ts
type Foo = Record<string, unknown>;对于此规则,使用 consistent-indexed-object-style: ["error", "index-signature"] 时,正确 的代码示例如下:
ts
interface Foo {
[key: string]: unknown;
}
type Foo = {
[key: string]: unknown;
};配置
该规则接受以下字符串值之一:
"record"
设置为 record 时,会强制对索引对象类型使用 Record,例如 Record<string, unknown>。
"index-signature"
设置为 index-signature 时,会强制使用索引签名类型,例如 { [key: string]: unknown }。
如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"typescript/consistent-indexed-object-style": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/consistent-indexed-object-style": "error",
},
});bash
oxlint --deny typescript/consistent-indexed-object-style版本
此规则在 v0.4.2 中新增。
