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

Time:10-06

Consider this:

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

So, ERROR 1 is:

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

Hence, I did what has been told me and replaced object by Record<string, unknown> in baz. But, now I get ERROR 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)

So, what's the proper approach to avoid using object?

CodePudding user response:

When you use Record<string, any>, you don't get the error.
Just be aware that the object values are of type any instead of unknown: see 'unknown' vs. 'any'

interface Foo {
  foo: string;
}

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

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

Typescript Playground

CodePudding user response:

This is currently a known issue with TypeScript.

You should change Foo to be a type instead of an interface:

type Foo = {
  foo: string
};

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

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