2

I have declared an enum in typescript as follows:

enum MultiPhaseUnits {
    AMPERAGE = 'AMPERAGE',
    VOLTAGE = 'VOLTAGE',
    POWER_FACTOR = 'POWER_FACTOR',
    LINE_VOLTAGE = 'LINE_VOLTAGE'
}

and created an array of enum values as such:

this.multiPhaseUnits = [MultiPhaseUnits.AMPERAGE, MultiPhaseUnits.LINE_VOLTAGE, MultiPhaseUnits.POWER_FACTOR, MultiPhaseUnits.VOLTAGE];

My problem is that when I go to check if a string is included in this array, VScode says parameter of type string is not assignable to parameter of type MultiPhaseUnit. For example:

if (this.multiPhaseUnits.indexOf(queryable.unit.apiUnit) > -1) {...}

It puts a red squiggly under queryable.unit.apiUnit, where apiUnit is a string in the form AMPERAGE, LINE_VOLTAGE.... I have verified this code will work and verified that the array multiPhaseUnits[] will end up essentially being an array of strings. Why is VScode complaining about this check? Even though my code works, if I am doing something wrong, how can I fix it?

1 Answer 1

1

You need to convert the string to type of MultiPhaseUnits.

if (this.multiPhaseUnits.indexOf(MultiPhaseUnits[queryable.unit.apiUnit]) > -1)
Sign up to request clarification or add additional context in comments.

1 Comment

That was it! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.