loading...

Deprecated Decorator

shadowtime2000 profile image shadowtime2000 ・1 min read

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!")
    }
}

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;
    }
}

Discussion

pic
Editor guide