Created
April 23, 2015 13:17
-
-
Save michelsalib/cfff8114ce081930a453 to your computer and use it in GitHub Desktop.
Typescript @deprecated annotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dog { | |
@deprecated('Dogs don\'t roar, they talk.') | |
roar() { | |
console.log('RRRRR'); | |
} | |
@deprecated() | |
smile() { | |
console.log('smile'); | |
} | |
walk() { | |
console.log('walk'); | |
} | |
} | |
function deprecated(message: string = 'Function {name} is deprecated.') { | |
return (instance, name, descriptor) => { | |
var original = descriptor.value; | |
var localMessage = message.replace('{name}', name); | |
descriptor.value = function() { | |
console.warn(localMessage); | |
return original.apply(instance, arguments); | |
}; | |
return descriptor; | |
}; | |
} | |
// create my dog | |
var d = new Dog(); | |
d.roar(); | |
d.smile(); | |
d.walk(); |
Does not work with Promise.
This works great with functions, but it does not work with classes.
Another issue, the line 25
uses instance
which will cause this
issue. Change it to this
will be OK:
export default function deprecated(message: string = 'Function {name} is deprecated.') {
return (instance: any, name: string, descriptor: any) => {
console.log('ins for ', name, ' = ', instance);
let original = descriptor.value;
let localMessage = message.replace('{name}', name);
descriptor.value = function() {
console.warn(localMessage);
// Use `this` instead of `instance`:
return original.apply(this, arguments);
};
return descriptor;
};
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This look good, but it does not work with Promise.