Skip to content

Instantly share code, notes, and snippets.

@41y08h
Created June 3, 2021 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 41y08h/bc57e4cd2e84bf34767e6e1ee210f670 to your computer and use it in GitHub Desktop.
Save 41y08h/bc57e4cd2e84bf34767e6e1ee210f670 to your computer and use it in GitHub Desktop.
typescript decorators
// @experimentalDecorators
let isAuthenticated = false;
function authenticate() {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const handler = descriptor.value
descriptor.value = (...args: any[]) => {
if(isAuthenticated) return handler(...args)
const value = prompt("login required")
if(value === 'login') {
isAuthenticated = true
handler(...args)
}
}
};
}
class Handler {
@authenticate()
static authenticated(req: string) {
console.log(req)
}
}
Handler.authenticated("give me some interfaces")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment