[Solved] Easily solve error TS2339: Property XXX does not exist on type
Tags: javascript, programming, typescript, errorts2339
Published: 2024-02-13 11:36:43

---
title: [Solved] Easily solve error TS2339: Property XXX does not exist on type
published: true
date: 2023-07-20 16:07:38 UTC
tags: Javascript,Programming,Typescript,errorTS2339
canonical_url: 
---

Are you an Angular Developer or Angular frontend Designer and facing this issue? Worry not in this blog you will find the complete guide on ” **error TS2339: Property XXX does not exist on type** “.

This is one of the common issues faced by beginners in typescript we can also in javascript too. In this article, we will talk about what is the reason for “error TS2339: Property XXX does not exist on type” and also various ways to solve this issue

## What is Angular Js?

Angular Js is a frontend framework used to design websites. It easily got recognition just in few years after it launched. It was developed by Google and Individuals through open-source contributions. Angular Js is an open-source framework and it was/is managed by the community. It was one of the most popular javascript frameworks and now it’s replaced by React JS. But the sad news Angular Js is now retired.

Note: [Angular JS has been Discontinued](https://docs.angularjs.org/misc/version-support-status#:~:text=AngularJS%20support%20has%20officially%20ended,for%20the%20actively%20supported%20Angular.). There will be no further development for Angular JS 

Even though it has been retired but still a lot of companies use it since it has a very good community and documentation.

## **Why do we get Property XXX does not exist on type**?

The simplest way to describe is you are trying to access an element that does not belong, like accessing an element in an array that does not exist.

Let’s take an example…

```
this.scene.manager.scenes.forEach((i) => {
  if(i.refresh){
    i.refresh();
  }
});
```

Here you are traversing through each element by using _ **forEach** _ function.

The error is in “i.refresh” of the if condition because here you trying to access the element rather than checking whether it belongs or not.

This can be easily solved just by changing the condition to something **_“refresh” in i_** this condition will return **True or False** which signifies whether it exists or not.

The Final Code will look something like this…

## **Solution 1:** Using Type Assertion

```
this.scene.manager.scenes.forEach((i) => {
  if ('refresh' in i) {
    (i as any).refresh();
  }
});
```

This is called Type Assertion in TypeScript. It’s one of the most widely used concepts in programming in conditional logic.

## What is Type Assertion in TypeScript?

In Layman’s terms, Type Assertion is a technique used to tell the type of variables to the Compiler. There are multiple advantages of type assertion one is to tell the compiler not to infer the type of variables.

Note: Type Assertion is different from Type Casting. Read in Detail from [here](https://www.javatpoint.com/typescript-type-assertion#:~:text=Type%20assertion%20is%20explicitly%20telling,code%20from%20JavaScript%20to%20TypeScript.) 

**How do Type Assertions?**  
Type assertion can be easily done by using **<>** or **as** operator

**Also Read:**

> [[Updated] Create Kali Linux Live USB with Persistence storage 2023](https://pratikpathak.com/kali-linux-live-usb-persistence/)

<iframe sandbox="allow-scripts" security="restricted" title="“[Updated] Create Kali Linux Live USB with Persistence storage 2023” — Pratik Pathak" src="https://pratikpathak.com/kali-linux-live-usb-persistence/embed/#?secret=wVwxvCF5jK#?secret=Bxv9yOrsYB" data-secret="Bxv9yOrsYB" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>

_https://pratikpathak.com/kali-linux-live-usb-persistence/_

## Solution 2: Using the Inbuilt Method

As I mentioned above the **error TS2339: Property XXX does not exist on type** is due to accessing an unidentified object in an array that is not available, it can easily be solved by checking first whether an element is available in the array/dictionary or not and then try accessing it.

This can be easily achieved by just replacing **_i.refresh_** with **_i[“refresh”]_**

the final code will look something like this

```
this.scene.manager.scenes.forEach((i) => {
  if(i["refresh"]){
    i["refresh"]();
  }
});
```

so here writing **_i[“refresh”]_** in if statement solves since if the **_i[“refresh”]_** returns any error or anything then the content of if block won’t run.

## Conclusion:

Here we saw two methods to handle the **error TS2339: Property XXX does not exist on type**. My personal suggestion will be to use Solution 1: Type Assertion since it is something that should be common practice for all coders, if you develop the habit of using type Assertion it will be beneficial for you and your code. It’s not very difficult and easy to understand.

If you still facing any issues please let them know in the comment section…