Skip to content
← Back to rules

typescript/no-generated-empty-object-type 可疑

💭 This rule requires type information.

作用 ​

禁止解析为空对象类型 {} 的类型操作。 这包括泛型类型引用和交叉类型,但不包括其键尚未解析的映射类型。

为什么这是不好的做法? ​

空对象类型 {} 接受任何非 nullish 值,包括字符串和数字等原始值。因此,使用工具类型意外生成此类型可能会允许原本不应允许的值。

此规则检查生成的类型。使用 typescript/no-empty-object-type 可禁止显式写出的空对象类型。

示例 ​

此规则的错误代码示例:

ts
type Data = { name: string; value: number };
type Empty = Omit<Data, "name" | "value">;
type NoProperties = Pick<Data, never>;
type NonNullish = NonNullable<unknown>;

此规则的正确代码示例:

ts
type Data = { name: string; value: number };
type WithValue = Omit<Data, "name">;
type WithOther = Omit<Data, "name" | "value"> & { other: string };

type Keys<T> = T extends infer U ? keyof U : never;
type Mapped<T extends object> = { [Key in Keys<T>]: Key };
type Referenced<T extends object> = Mapped<T>;

如何使用 ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/no-generated-empty-object-type": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/no-generated-empty-object-type": "error",
  },
});
bash
oxlint --type-aware --deny typescript/no-generated-empty-object-type

版本 ​

此规则在 vnext 中添加。

参考 ​