1

I have method export const getMatchPicks = (match: IMatch, ends: EndType[]): IPick[] => ends.map(end => matchToPick(match, end));

Also, I have:

enum EndType {
    HOME = 'home',
    DRAW = 'draw',
    AWAY = 'away',
}

My goal call getMatchesPicksList function passing EndType enums as array:

 getMatchesPicksList(matches, [EndType.HOME, EndType.AWAY, EndType.DRAW])

The code above has been compiled correctly. But what if I will have n length enum? How to pass it to function?

I expect something like getMatchesPicksList(matches, EndType) , but it returns:

error TS2345: Argument of type 'typeof EndType' is not assignable to parameter of type 'EndType[]'. Property 'includes' is missing in type 'typeof EndType'.

1
  • so, you want a way to get all the values from a string enum? Commented Feb 28, 2018 at 20:24

1 Answer 1

1

Well, getMatchesPicksList() doesn't take the EndType enum object directly. It takes an array of property values of the EndType enum object. It is a bit confusing that EndType refers to a value (the enum object) as well as a type (the string property values, something like the union 'home' | 'draw' | 'away').

If you want to turn a string enum object into an array of its property values, you could make a function to do it:

function getStringEnumValues<E extends Record<keyof E, string>>(e: E): E[keyof E][] {
  return (Object.keys(e) as (keyof E)[]).map(k => e[k]);
}

And then call your function on its result:

getMatchesPicksList(matches, getStringEnumValues(EndType));

If you are trying to do something else, please give more information. Hope that helps; good luck!

Sign up to request clarification or add additional context in comments.

Comments

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.