0

When should I create enum or use string literal type ?

String literal type:

export type Person = {
    id : number,
    name : string,
    gender : "male" | "female"
}

Enum:

export enum Gender{
    male = "male",
    female = "female"
}

export type Person = {
    id : number,
    name : string,
    gender : Gender
}

Which one should I use and why ? Is it any best practices or convention ? I don't see big difference if the enum is used by 1 class and 1 component.

0

1 Answer 1

-1

you can use enum for set of names constant for example you have you want to know the direction so you can store that in enum

String literal allow you to specify exact string values that a variable can have like this : type Status = "active" | "inactive" | "pending";

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.