Fireship · @fireship
The Magic of TypeScript Decorators
Uploaded February 5, 2019 · Length 9:51
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.
- 0:00
[Music]
- 0:06
decorator's are one of the coolest
- 0:08
features in typescript we see them used
- 0:09
all over the place in frameworks like
- 0:11
angular mob X and stencil to help us
- 0:13
write more elegant code but have you
- 0:15
ever created your own from scratch if
- 0:16
not today's video is all about the
- 0:18
practical applications of typescript
- 0:20
decorators will look at a whole bunch of
- 0:21
different examples and will even go so
- 0:23
far as to implement an API that looks
- 0:25
similar to react hooks in angular and
- 0:27
will do it with only five lines of code
- 0:28
if you're new here like and subscribe
- 0:30
and you can find the full source code on
- 0:32
fire ship IO so what is it that grater
- 0:34
exactly it's just a function that allows
- 0:36
you to hook into your source code and
- 0:38
either extend the functionality of it or
- 0:40
annotate it with metadata so why would
- 0:42
you ever want to annotate your code with
- 0:43
metadata the answer is you probably
- 0:45
don't there might be some good use cases
- 0:47
out there but it comes more into play if
- 0:48
you're building something like a
- 0:49
compiler like the angular compiler for
- 0:51
example and you need to analyze the
- 0:53
metadata to do things like dependency
- 0:55
injection so decorators can provide this
- 0:57
metadata but they can also directly hook
- 0:59
into your code and alter the behavior of
- 1:01
it and that's what's really useful to
- 1:02
the average app developer because it
- 1:04
allows you to write abstractions that
- 1:05
are clear and concise but they're almost
- 1:07
too good at creating abstractions so you
- 1:09
need to be careful not to overuse
- 1:11
decorators it can be tempting to create
- 1:13
a new decorator for every little thing
- 1:15
but they should be reserved for logic
- 1:16
that's stable and that needs to be
- 1:18
reused frequently throughout the app
- 1:19
there are five different things that you
- 1:21
can decorate class definitions methods
- 1:23
properties getters and setters and
- 1:25
parameters we'll be looking at examples
- 1:28
of all these throughout the video but
- 1:29
basically the function that you
- 1:31
implement will be dependent on the thing
- 1:33
that you're decorating in other words
- 1:34
the function arguments required to
- 1:36
decorate a class will be different from
- 1:37
the arguments required to decorate a
- 1:39
method I'll be using angular for this
- 1:41
demo so we have some actual working
- 1:42
examples and you can see here I'm in a
- 1:44
component file and I've simply taken out
- 1:46
the component decorator and replaced it
- 1:48
with this frozen decorator basically
- 1:51
what we want to do is prevent this class
- 1:52
from being extended when you're
- 1:54
extending a class with inheritance you
- 1:56
need to be careful with decorators
- 1:57
because the inherited class won't
- 1:59
actually receive the functionality of


