DEV Community

shadowtime2000
shadowtime2000

Posted on

Deprecated Decorator

This is the start of a series I am creating with various useful Typescript decorators.

You can track the progress of the series on Github.

The Decorator

I want this decorator to be used like this:

class MyClass {

    @Deprecated()
    doSomething() {
        console.log("DO SOMETHING!")
    }
}
Enter fullscreen mode Exit fullscreen mode

Source

function Deprecated(): MethodDecorator {
    return (target: Object, key: string | symbol, descriptor: PropertyDescriptor) => {
        const original = descriptor.value;
        descriptor.value = (...args: any) => {
            console.warn(`Warning: ${String(key)} is deprecated`);
            original(...args);
        }
        return descriptor;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)