This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In


    TypeScript - Understanding Decorators

    John LindquistJohn Lindquist

    Decorators are a feature of TypeScript that are becoming more and more common in many major libraries. This lesson walks you through what decorators are and how to create your own.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    00:00 If I have a person and they have a name of John, I can decorate that person by doing something like adding an age. I'll say, function, add age. This will take an age. This is a add age decorator, a function that's going to take some sort of configuration and then can decorate the next thing we'll pass in.

    00:26 To be able to pass in something else, we'll return another function. This function is going to take the person that gets passed in. Basically, you'll call this by invoking add age with the age, we'll say the age is 30, and then since this returns a function, you could pass in the person here.

    00:46 It goes age, person, then age, person. What we need to return from this function is something that matches that original person object with our new stuff added, so we'll go ahead and return an object that has the age and then a name of When we call this, I'll just say new person, and I'll log out this new person.

    01:13 Hit save, refresh. You can see, we get age 30, name John. Again, it's age, person, then age, person, and then the object it returns because ES 6 supports this. If the key in value is the same name, you don't have to include the value, so it'll just automatically assign age, and then we'll grab the name of the person.

    01:34 We've effectively decorated this person with an age. There's a new syntax proposed for decorators, which we'll need to enable experimental decorators for this. I'll set this to true in my tsconfig. I'm going to delete all this and create a new person which has a name of John.

    02:00 Then I'll create a decorator, basically that same add age decorator I created before, that takes an age that returns a function, but this time instead of an instance of the person, it's going to have a target class, meaning I'll have to call new on this. The way I can use this now is, I can say, add age 30. Then anytime I create a new person, it's going to apply this decorator to it.

    02:31 We can see that instead of returning an object, I'm going to return a class where the name is the new target, and the age is the age that got passed in. With this syntax, I can now say new person. If I just log out this new person, you'll see when I refresh, I'll get class 1, name John, aged 30.

    03:06 If I want to change the age of this person, let's say 20, refresh, you can see I have an age of 20. Every time I create a new instance of this person it's going to add this decorator to it. To use these decorators, because it's an ES 7-proposed feature, you'll need to enable experimental decorators.