Menu

πŸ“°
0

Build a Logger and Validator with TypeScript Decorators (Like NestJS)

DEV Community: nestjsΒ·Malloc72PΒ·about 1 month ago
#awl2CNMO
#dev#class#code#highlight#decorator#article
Reading 0:00
15s threshold

Note: This post is a translated version of an article originally published on my personal blog. You can read the original Korean post here . What Are Decorators? Decorators are a metaprogramming syntax that lets you attach metadata β€” or extend behavior β€” on classes, methods, properties, and parameters without modifying the original code . They're commonly used for: Logging β€” track when methods are called Validation β€” enforce constraints on class properties Dependency Injection β€” wire up services automatically Authorization β€” guard methods behind permission checks Frameworks like Angular and NestJS lean heavily on decorators for all of the above. Setting Up First, enable decorators in your tsconfig.json : { "compilerOptions" : { "experimentalDecorators" : true } } How Decorators Work A decorator is just a function. When you apply it to a class or method, TypeScript calls that function at definition time with some useful arguments.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More