TypeScript. Decorators

Home » Tutorials » JavaScript » TypeScript. Decorators
In this tutorial we’ll discuss decorators in TypeScript and consider some its use examples.
Decorators are experimental but very powerful part of TypeScript language. Decorators are very important part of such a javascript framework – Angular. In TypeScript decorator is function, which can add some meta information or change entity’s behavior, which this decorator applies (class, class method, class property etc).

Код урока

function myDecorator(target: Function): any {
    const newConstructor: Function = function (name: string) {
        this.name = 'Hello, ' + name;
        setInterval(() => console.log(1), 1000);
    };
    return newConstructor;
}

function sumDecorator(target: Object, method: string) {
    console.log(target);
    console.log(method);
}

@myDecorator
class Programmer {
    lang = 'typescript';
    name: string;

    constructor(name) {
        this.name = name;
    }

    // @ts-ignore
    // @sumDecorator
    add(a: number, b: number) {
        return a + b;
    }
}

let kamil = new Programmer('Kamil');
console.log(kamil);

const res = kamil.add(1, 2);
console.log(res);

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Pin It on Pinterest

Share This