Home > OS >  What type is a function that returns an enum member?
What type is a function that returns an enum member?

Time:08-03

I don't know what would be the type of the function that returns an enum member. Here is my code so far:

interface Letter {
    character: string,
    color: Function
}

enum Color {
    Green,
    Yellow,
    Grey
}

const letter_1: Letter = {
    character: playerInput[0],
    color: () => {
        if (letter_1.character === playerInput[0])
            return Color.Green;
            
        else {
            for (let i = 0; i < playerInput.length; i   ) {
                if (letter_1.character === playerInput[i])
                    return Color.Yellow;
            }
        }
         
        return Color.Grey;
    }
};

In the Letter interface for now I set the type of color() to Function but I think there must be another type for it that I just don't know.

CodePudding user response:

The type you're looking for is () => Color, i.e. a no-argument function that returns a Color enum value:

interface Letter {
    character: string,
    color: () => Color
}

enum Color {
    Green,
    Yellow,
    Grey
}

const letter: Letter = {
    character: '',
    color: () => Color.Green
};
  • Related