3 ms·
What would you use for a field called "stage" that could be set to 'dev' or 'stage' or 'prod'? Or other enum-type things? I've seen pure JS enums like {'dev' :
by xsmasher 4y ago
What would you use for a field called "stage" that could be set to 'dev' or 'stage' or 'prod'? Or other enum-type things?
I've seen pure JS enums like {'dev' : Symbol('dev'), 'prod' : etc} but I never use them because you can't send them over the wire.
You could use a type like {dev:true} | {prod:true} | {stage:true} but you still wind up comparing strings.
- mmis1000 4y ago> {dev:true} | {prod:true} | {stage:true} This can't be accessed safely. Because all value in typescript can be sub type of how it typed. It means code following will pass. var a = { dev: true, get prod() { throw Error() } } var b: { dev:true } | {prod:true} | {stage:true} = a And there is no guarantee that access any of these fields is safe.
- xsmasher 4y agoThat did not compile for me with my TS settings; > Type '{ dev: boolean; readonly prod: void; }' is not assignable to type '{ prod: true; }' It's still a bad solution though; it allows nonsense values like {dev:true, prod:true}.
- mmis1000 4y agoThat is because I forgot to assert the true as literal true, so it defaults to be boolean (where both true and false are allowed)