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.
One reply on “How to fix the error “TS2339: Property ‘x’ does not exist on type ‘Y'” with TypeScript?”
you are a hero, thank you so much