如何用 `Record<string, unknown>` 替换 `object`?

How to replace `object` with `Record<string, unknown>`?

考虑一下:

interface Foo {
  foo: string;
}

const foo: Foo = { foo: "foo" };

function bar(obj: object) {}                   // <--- ERROR 1
bar(foo);

function baz(obj: Record<string, unknown>) {}
baz(foo);                                      // <--- ERROR 2

因此,错误 1 ​​是:

Don't use `object` as a type. The `object` type is currently hard to use ([see this issue](https://github.com/microsoft/TypeScript/issues/21732)).
Consider using `Record<string, unknown>` instead, as it allows you to more easily inspect and use the keys.eslint@typescript-eslint/ban-types

因此,我按照别人告诉我的去做,在 baz 中用 Record<string, unknown> 替换了 object。但是,现在我得到错误 2:

Argument of type 'Foo' is not assignable to parameter of type 'Record<string, unknown>'.
  Index signature for type 'string' is missing in type 'Foo'.ts(2345)

那么,避免使用 object 的正确方法是什么?

目前这是 known issue TypeScript。

您应该将 Foo 更改为 type 而不是 interface:

type Foo = {
  foo: string
};

const foo: Foo = { foo: "foo" };

function baz(obj: Record<string, unknown>) {}
baz(foo);                   

编辑:如果您对界面没有任何控制权,您可以使用由 Younho Choo 在上述 GitHub 问题线程上创建的实用程序类型:

interface Foo {
  foo: string;
}

type IndexSignature<O extends object> = {
  [P in keyof O]: O[P]
};

const foo: IndexSignature<Foo> = { foo: "foo" };

function baz(obj: Record<string, unknown>) {}
baz(foo); 

这避免了使用 any,一些开发人员认为这是不好的做法(尽管我认为在 TmTron 的回答中这是完全可以接受的做法)。

TypeScript Playground

当您使用 Record<string, any> 时,您不会收到错误。
请注意对象值的类型是 any 而不是 unknown:请参阅

interface Foo {
  foo: string;
}

const foo: Foo = { foo: "foo" };

function fct(obj: Record<string, any>) {}
fct(foo);

Typescript Playground