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.