Fireship · @fireship

The Magic of TypeScript Decorators

Uploaded February 5, 2019 · Length 9:51

The Magic of TypeScript Decorators

Summary

typescript decorators explained with examples, including class, method, and property decorators, and how to use them to create reusable and maintainable code.

  • typescript decorators
  • typescript examples
  • class decorators
  • method decorators
  • property decorators
  • typescript best practices
  • typescript development

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:06

    decorator's are one of the coolest

  3. 0:08

    features in typescript we see them used

  4. 0:09

    all over the place in frameworks like

  5. 0:11

    angular mob X and stencil to help us

  6. 0:13

    write more elegant code but have you

  7. 0:15

    ever created your own from scratch if

  8. 0:16

    not today's video is all about the

  9. 0:18

    practical applications of typescript

  10. 0:20

    decorators will look at a whole bunch of

  11. 0:21

    different examples and will even go so

  12. 0:23

    far as to implement an API that looks

  13. 0:25

    similar to react hooks in angular and

  14. 0:27

    will do it with only five lines of code

  15. 0:28

    if you're new here like and subscribe

  16. 0:30

    and you can find the full source code on

  17. 0:32

    fire ship IO so what is it that grater

  18. 0:34

    exactly it's just a function that allows

  19. 0:36

    you to hook into your source code and

  20. 0:38

    either extend the functionality of it or

  21. 0:40

    annotate it with metadata so why would

  22. 0:42

    you ever want to annotate your code with

  23. 0:43

    metadata the answer is you probably

  24. 0:45

    don't there might be some good use cases

  25. 0:47

    out there but it comes more into play if

  26. 0:48

    you're building something like a

  27. 0:49

    compiler like the angular compiler for

  28. 0:51

    example and you need to analyze the

  29. 0:53

    metadata to do things like dependency

  30. 0:55

    injection so decorators can provide this

  31. 0:57

    metadata but they can also directly hook

  32. 0:59

    into your code and alter the behavior of

  33. 1:01

    it and that's what's really useful to

  34. 1:02

    the average app developer because it

  35. 1:04

    allows you to write abstractions that

  36. 1:05

    are clear and concise but they're almost

  37. 1:07

    too good at creating abstractions so you

  38. 1:09

    need to be careful not to overuse

  39. 1:11

    decorators it can be tempting to create

  40. 1:13

    a new decorator for every little thing

  41. 1:15

    but they should be reserved for logic

  42. 1:16

    that's stable and that needs to be

  43. 1:18

    reused frequently throughout the app

  44. 1:19

    there are five different things that you

  45. 1:21

    can decorate class definitions methods

  46. 1:23

    properties getters and setters and

  47. 1:25

    parameters we'll be looking at examples

  48. 1:28

    of all these throughout the video but

  49. 1:29

    basically the function that you

  50. 1:31

    implement will be dependent on the thing

  51. 1:33

    that you're decorating in other words

  52. 1:34

    the function arguments required to

  53. 1:36

    decorate a class will be different from

  54. 1:37

    the arguments required to decorate a

  55. 1:39

    method I'll be using angular for this

  56. 1:41

    demo so we have some actual working

  57. 1:42

    examples and you can see here I'm in a

  58. 1:44

    component file and I've simply taken out

  59. 1:46

    the component decorator and replaced it

  60. 1:48

    with this frozen decorator basically

  61. 1:51

    what we want to do is prevent this class

  62. 1:52

    from being extended when you're

  63. 1:54

    extending a class with inheritance you

  64. 1:56

    need to be careful with decorators

  65. 1:57

    because the inherited class won't

  66. 1:59

    actually receive the functionality of