Categories
TypeScript Answers

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

Spread the love

To fix error TS2339: Property ‘x’ does not exist on type ‘Y’ with TypeScript, we need to define the property in the interface with the right type.

For instance, we write

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

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

to define the Images interface with the main property.

Then we can get the value of the images.main property without an error since the Images interface has the main property and it’s a string.

If the property name and its type matches, then the error should go away.

By John Au-Yeung

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

Leave a Reply

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