Skip to content
Advertisement

How to force a type in a variable : The property does not exist in the type ‘AllMatch | SingleMatch’ ts(2339)

I have an interface of this shape:

JavaScript

And I need to access the value field of the SingleMatch interface on a variable. To do this I use the following code:

JavaScript

In the buildMonoExpressionSingleMatch() function I can access my variable without error if I check before that the function is of the right type for index 0:

JavaScript

But if I do this check with the dynamic index I passed as a parameter (which I want to do):

JavaScript

I get an error that says:

JavaScript

How to tell the Typescript compiler: I know I’m in this type so this field must exist?

I tried adding a ! to my value field to remove undefined and null from my variable but the problem remained the same.

If you know how to get past this error tell me

If you see what I’m doing wrong it would help me a lot to get on the right track. Thank you in advance if you take the time to help me.

Advertisement

Answer

I found my answer, thanks to Dave Meehan’s comment.

By first retrieving my sub term, then reading the field I am interested in, I manage to read the field of my variable which can be of several different types.

The code looks like this:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement