Categories
TypeScript Answers

How to fix the error “TS2339: Property ‘x’ does not exist on type ‘Y'” with TypeScript?

Spread the love

Sometimes, we want to fix the error "TS2339: Property ‘x’ does not exist on type ‘Y’" with TypeScript.

In this article, we’ll look at how to fix the error "TS2339: Property ‘x’ does not exist on type ‘Y’" with TypeScript.

How to fix the error "TS2339: Property ‘x’ does not exist on type ‘Y’" with TypeScript?

To fix the error "TS2339: Property ‘x’ does not exist on type ‘Y’" with TypeScript, we should make sure the properties are listed in the interface that’s set as the type of the object.

For instance, we write

interface Images {
  main: string;
  [key: string]: string;
}

const getMainImageUrl = (images: Images): string => {
  return images.main;
};

to create the Images interface that has the main property.

Then we can access the images.main property in the getMainImageUrl function without TypeScript compiler errors.

Conclusion

To fix the error "TS2339: Property ‘x’ does not exist on type ‘Y’" with TypeScript, we should make sure the properties are listed in the interface that’s set as the type of the object.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

One reply on “How to fix the error “TS2339: Property ‘x’ does not exist on type ‘Y'” with TypeScript?”

Leave a Reply

Your email address will not be published. Required fields are marked *