cancel
Showing results for 
Search instead for 
Did you mean: 

error TS2339: Property does not exist on type

lfrey91
Participant
0 Kudos
11,380

Hi TypeScript Team,

I get 3 errors here where I think they are not errors

I am using

sap.m.IconTabHeader for .getSelectedKey

and

sap.ui.layout.VerticalLayout for .setVisible

in the console it is working

peter.muessig

do you have a quick fix for this?

bye Luis

Accepted Solutions (1)

Accepted Solutions (1)

AndreasKunz
Product and Topic Expert
Product and Topic Expert

Hi Luis,

the error message already indicates it: TypeScript does not find these methods on UI5Element (which stands for the sap.ui.base.Element base class of all Controls etc.).

The TypeScript compiler simply is not aware that this.byId(...) returns an IconTabHeader. It cannot know this, as the returned control type depends on the ID which you give as argument (and there is no functionality which searches the id in the XMLView and tells TypeScript what kind of control has this ID).

The solution is to cast the result of this call to the control type you know is returned:

(this.byId("idIconTabBar") as IconTabBar).getSelectedKey()

This is commonly done in the tutorial and samples linked from the central UI5 & TypeScript entry point (https://sap.github.io/ui5-typescript), so I guess it makes sense to mention this page for those who are getting here and not aware of it.

Regards

Andreas

lfrey91
Participant
0 Kudos

Great that worked!

Answers (2)

Answers (2)

guilherme_sales
Product and Topic Expert
Product and Topic Expert

Hi,

This error is because `byId` returns a `UI5Element` (a base class from which all view controls extend), and that doesn't have the methods you are trying to call. Try casting the element to the control class you are actually using in the view.

const iconTabHeader = this.byId('idIconTabHeader') as IconTabHeader
switch(iconTabHeader.getSelectedKey()) { ... }

For the `setVisible` method it should cast as at least the 'Control' class.

const c = this.byId('id') as Control
c.setVisible(true)

It works in the console because that's pure javascript, which is less strict than typescript. In reality your code could run without any errors, but typescript doesn't allow you to compile the code without checking types.

yogananda
Product and Topic Expert
Product and Topic Expert
0 Kudos

Luis Frey

This error indicates that you have used a property that does not exist in the type of object you are trying to access. Check the object type you are accessing and make sure the property you are trying to access is defined for that type. If it is not, you will need to either change the property you are trying to access or the type of object you are trying to access.

lfrey91
Participant
0 Kudos

Hi yoganandamuthaiah ,

thanks for your quick reply.

i checked it again:

https://sapui5.hana.ondemand.com/#/api/sap.m.IconTabHeader%23methods/Summary