Published: Oct. 5, 2020, 1 p.m.
b'
In this Hasty Treat, Scott and Wes talk about enums in JS \\u2014 what they are, what they do, and how they work in JavaScript.
LogRocket - Sponsor
LogRocket lets you replay what users do on your site, helping you reproduce bugs and fix issues faster. It\\u2019s an exception tracker, a session re-player and a performance monitor. Get 14 days free at\\xa0logrocket.com/syntax.
Show Notes
03:11 - What are enums?
- A type restricting variables to one value from a predefined set of constants
- Direction = UP / DOWN / LEFT / RIGHT
- Role = ADMIN EDITOR AUTHOR VIEWER
- Day of the Week
05:12 - In GraphQL
- enum Role { ADMIN EDITOR AUTHOR VIEWER}
- Then
07:05 - In TypeScript
- First, declare the type:
- enum direction = { UP, DOWN, LEFT, RIGHT }
- Then when you defined your function, use that type
- type User { role: Role } or type User { role: [Role] }
08:49 - In JavaScript
- Not in JS yet
- There is a proposal in stage 1
- And a babel plugin:
- Can use case/switch
- Can use Object or Map keys
Links
Tweet us your tasty treats!
'