Created
June 3, 2021 05:27
-
-
Save 41y08h/bc57e4cd2e84bf34767e6e1ee210f670 to your computer and use it in GitHub Desktop.
typescript decorators
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
// @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