Answer the question
In order to leave comments, you need to log in
When to use Enum and when to use Union?
https://stackoverflow.com/questions/18111657/how-t...
https://stackoverflow.com/questions/40275832/types...
export enum SocialAuthService {
FACEBOOK = 'facebook',
GOOGLE = 'google',
}
type SocialAuthServiceType =
| SocialAuthService.FACEBOOK
| SocialAuthService.GOOGLE
;
someFunc(service: SocialAuthServiceType){
..
}
const Permission = {
Read: 'r',
Write: 'w',
Execute: 'x'
} as const;
Answer the question
In order to leave comments, you need to log in
Union when only the type is needed, enum when the runtime values are also needed. It's obvious. A const object is stupidly not as convenient as an enum, there is no point in it for this class of tasks.
In general, strictly speaking, enum should be used when enum is needed. L - logic. Enum - Enumeration - Enumeration. There is an enumeration - use enum.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question