Syntax - Tasty Web Development Treats

Hasty Treat - Enums in JS (GraphQL and Typescript)


Listen Later

In this Hasty Treat, Scott and Wes talk about enums in JS — 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’s an exception tracker, a session re-player and a performance monitor. Get 14 days free at logrocket.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
    • https://github.com/rbuckton/proposal-enum
  • And a babel plugin:
    • https://www.npmjs.com/package/babel-plugin-const-enum
  • Can use case/switch
  • Can use Object or Map keys
Links
  • Syntax 287: Hasty Treat - Records and Tuples in JavaScript
Tweet us your tasty treats!
  • Scott’s Instagram
  • LevelUpTutorials Instagram
  • Wes’ Instagram
  • Wes’ Twitter
  • Wes’ Facebook
  • Scott’s Twitter
  • Make sure to include @SyntaxFM in your tweets
...more
View all episodesView all episodes
Download on the App Store

Syntax - Tasty Web Development TreatsBy Wes Bos & Scott Tolinski - Full Stack JavaScript Web Developers

  • 4.9
  • 4.9
  • 4.9
  • 4.9
  • 4.9

4.9

966 ratings


More shows like Syntax - Tasty Web Development Treats

View all
Hanselminutes with Scott Hanselman by Scott Hanselman

Hanselminutes with Scott Hanselman

377 Listeners

Software Engineering Radio - the podcast for professional software developers by se-radio@computer.org

Software Engineering Radio - the podcast for professional software developers

265 Listeners

.NET Rocks! by Carl Franklin and Richard Campbell

.NET Rocks!

245 Listeners

The Changelog: Software Development, Open Source by Changelog Media

The Changelog: Software Development, Open Source

285 Listeners

Thoughtworks Technology Podcast by Thoughtworks

Thoughtworks Technology Podcast

41 Listeners

Talk Python To Me by Michael Kennedy

Talk Python To Me

586 Listeners

Software Engineering Daily by Software Engineering Daily

Software Engineering Daily

629 Listeners

Soft Skills Engineering by Jamison Dance and Dave Smith

Soft Skills Engineering

275 Listeners

Python Bytes by Michael Kennedy and Brian Okken

Python Bytes

213 Listeners

The freeCodeCamp Podcast by freeCodeCamp.org

The freeCodeCamp Podcast

485 Listeners

CoRecursive: Coding Stories by Adam Gordon Bell - Software Developer

CoRecursive: Coding Stories

186 Listeners

The Stack Overflow Podcast by The Stack Overflow Podcast

The Stack Overflow Podcast

63 Listeners

The Real Python Podcast by Real Python

The Real Python Podcast

136 Listeners

PodRocket - A web development podcast from LogRocket by LogRocket

PodRocket - A web development podcast from LogRocket

58 Listeners

The Pragmatic Engineer by Gergely Orosz

The Pragmatic Engineer

52 Listeners