Categories
TypeScript Answers

How to fix the “property does not exist on ‘Object'” error in TypeScript?

Spread the love

Sometimes, we want to fix the "property does not exist on ‘Object’" error in TypeScript.

In this article, we’ll look at how to fix the "property does not exist on ‘Object’" error in TypeScript.

How to fix the "property does not exist on ‘Object’" error in TypeScript?

To fix the "property does not exist on ‘Object’" error in TypeScript, we can define an interface and use that as the type instead of using the Object type.

For instance, we write

interface Options {
  selector?: string;
  template?: string;
}

const doStuff = (o: Options) => {
  //...
};

doStuff({});
doStuff({ selector: "foo" });

to add the Options interface that has some optional properties inside.

Then we use that as the type for the o parameter in doStuff.

Finally, we can call it with some objects that matches the structure of the Options interface.

Conclusion

To fix the "property does not exist on ‘Object’" error in TypeScript, we can define an interface and use that as the type instead of using the Object type.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.