typescript enums vs unions
Typescript enums have their uses but are extra hassle when creating new dto instances. I don't think enums are a good fit for working with dtos.
When I try to instantiate a new AutoTagsConfigType instance as mock data for tests, typescript is reporting an error for the status
field because it's defined as an ATStatus
enum
So setting status to a literal like this is not acceptable to typescript
const mockData: AutoTagsConfigType = {
status: 'Pending',
id: 'd3f963fb-2201-4f5c-ae38-21bf22cec001',
name: 'Big Spender',
}
but setting status as: status: ATStatus.Pending
does work
however that's not very nice to have to do (use the enum type all the time, I prefer using literals)
especially if typescript has another way of achieving the same goal (limit status values) using unions
if ATStatus is a union not an enum, then setting values literally works as we would like and typescript will only allow set one of those valuestype ATStatus = 'Ready' | 'Pending' | 'Onboarding' | 'Disabled';
So union makes more sense to me.
Having said that, here's something to consider which combines best of both worlds
- define the enum as before:
export enum ATStatus { Ready = 'Ready', Pending = 'Pending', Onboarding = 'Onboarding', Disabled = 'Disabled', }
- use
keyof typeof
to create a new union type: type ATStatusOther = keyof typeof ATStatus;
- use ATStatusOther as the status field type:
status: ATStatusOther
- and now you can set literal values on the status field
status: 'Pending'
. - OR set using the enum
status: ATStatus.Ready
(wow!)
warning:
keyof typeof
only picks up the value of the enum name so if initialized to different value then it won't work (in ATStatus case the name of the enum values and the actual literal value are the same so it works).
Comments
Post a Comment