While working with NestJS, I was amazed at how extensively it leverages decorators, even though JavaScript doesn't fully support them yet. This made me curious about how NestJS enables decorator support. I then researched how TypeScript implements decorators and whether NestJS follows the same approach or relies on its own custom implementation. In this article, we'll explore exactly that.
Introduction
NestJS provides a powerful abstraction for building scalable applications, leveraging decorators to simplify and structure code. However, understanding how these decorators work internally can help in optimizing performance, debugging issues, and even creating custom decorators effectively.
This article takes a deep dive into:
- How TypeScript decorators work
- The current state of JavaScript decorator support
- How NestJS leverages
reflect-metadatato enable powerful decorator functionality - How to create and use custom decorators
- Performance considerations when using decorators
1. Understanding TypeScript Decorators
What Are Decorators?
Decorators are special functions that modify the behavior of classes, methods, properties, or
parameters they are attached to. They are essentially metadata annotations attached using the
@ symbol.
The Current State of Decorators in JavaScript
JavaScript doesn't fully support decorators in its standard yet because:
- Decorators are still a Stage 3 proposal in ECMAScript (as of March 2025)
- The proposal has gone through several iterations and design changes
- Browser implementations are still in progress
However, TypeScript has supported decorators as an experimental feature for years, enabling frameworks like NestJS to leverage them.
How TypeScript Implements Decorators
TypeScript supports decorators through its compiler, which transforms decorator syntax into regular JavaScript function calls.
Let's look at a simple example:
/ / D e c o r a t o r f a c t o r y - r e t u r n s t h e a c t u a l d e c o r a t o r f u n c t i o n
f u n c t i o n L o g ( ) {
/ / T h e a c t u a l d e c o r a t o r f u n c t i o n
r e t u r n f u n c t i o n ( t a r g e t : a n y , p r o p e r t y K e y : s t r i n g , d e s c r i p t o r : P r o p e r t y D e s c r i p t o r ) {
c o n s t o r i g i n a l M e t h o d = d e s c r i p t o r . v a l u e ;
/ / R e p l a c e t h e o r i g i n a l m e t h o d w i t h a n e w o n e t h a t i n c l u d e s l o g g i n g
d e s c r i p t o r . v a l u e = f u n c t i o n ( . . . a r g s : a n y [ ] ) {
c o n s o l e . l o g ( ` C a l l i n g $ { p r o p e r t y K e y } w i t h ` , a r g s ) ;
r e t u r n o r i g i n a l M e t h o d . a p p l y ( t h i s , a r g s ) ;
} ;
} ;
}
c l a s s U s e r {
@ L o g ( )
g e t U s e r ( n a m e : s t r i n g ) {
r e t u r n ` U s e r : $ { n a m e } ` ;
}
}
c o n s t u s e r = n e w U s e r ( ) ;
u s e r . g e t U s e r ( " T e j a s " ) ; / / L o g s : " C a l l i n g g e t U s e r w i t h [ " T e j a s " ] " t h e n r e t u r n s " U s e r : T e j a s "
Different Types of Decorators
TypeScript supports five types of decorators:
- Class decorators - Applied to the class constructor
- Method decorators - Applied to methods
- Property decorators - Applied to properties
- Parameter decorators - Applied to function parameters
- Accessor decorators - Applied to getters/setters
Each type of decorator receives different parameters and is used for different purposes in NestJS.
What Happens During Compilation?
When TypeScript compiles decorators, it transforms them into function calls that wrap around the original code. Here's a simplified version of what the compiled JavaScript might look like for our example:
/ / S i m p l i f i e d c o m p i l e d o u t p u t
v a r U s e r = ( f u n c t i o n ( ) {
f u n c t i o n U s e r ( ) { }
/ / O r i g i n a l m e t h o d
U s e r . p r o t o t y p e . g e t U s e r = f u n c t i o n ( n a m e ) {
r e t u r n " U s e r : " + n a m e ;
} ;
/ / A p p l y d e c o r a t o r
U s e r . p r o t o t y p e . g e t U s e r = ( f u n c t i o n ( o r i g i n a l M e t h o d ) {
r e t u r n f u n c t i o n ( . . . a r g s ) {
c o n s o l e . l o g ( " C a l l i n g g e t U s e r w i t h " , a r g s ) ;
r e t u r n o r i g i n a l M e t h o d . a p p l y ( t h i s , a r g s ) ;
} ;
} ) ( U s e r . p r o t o t y p e . g e t U s e r ) ;
r e t u r n U s e r ;
} ) ( ) ;
This transformation happens at compile-time, but the decorator's effects occur at runtime.
2. How NestJS Uses Decorators Internally
NestJS Decorators & reflect-metadata
NestJS extends TypeScript's decorator capabilities using a library called reflect-metadata. This
library implements a proposal for adding a Reflection API to JavaScript, allowing frameworks to
store and retrieve metadata about classes, methods, and properties.
What Exactly is reflect-metadata?
reflect-metadata is a polyfill library that implements the Metadata Reflection API proposal. It
allows you to:
- Define metadata on classes and class members
- Retrieve metadata at runtime
- Use type information from TypeScript (with some compiler options enabled)
This is crucial for NestJS's dependency injection system, which needs to know about types to instantiate them correctly.
Example: Using reflect-metadata in NestJS
i m p o r t " r e f l e c t - m e t a d a t a " ; / / M u s t b e i m p o r t e d o n c e a t t h e a p p l i c a t i o n e n t r y p o i n t
/ / C r e a t e a m e t a d a t a k e y
c o n s t M E T A _ K E Y = " r o l e " ;
/ / D e c o r a t o r f a c t o r y
f u n c t i o n R o l e ( r o l e : s t r i n g ) {
/ / T h e a c t u a l d e c o r a t o r
r e t u r n f u n c t i o n ( t a r g e t : O b j e c t , k e y ? : s t r i n g | s y m b o l ) {
/ / S t o r e m e t a d a t a o n t h e t a r g e t
R e f l e c t . d e f i n e M e t a d a t a ( M E T A _ K E Y , r o l e , t a r g e t , k e y ) ;
} ;
}
c l a s s U s e r {
@ R o l e ( " a d m i n " )
a c c e s s D a s h b o a r d ( ) {
/ / M e t h o d i m p l e m e n t a t i o n
}
}
/ / L a t e r , r e t r i e v e t h e m e t a d a t a
c o n s t u s e r = n e w U s e r ( ) ;
c o n s t r o l e = R e f l e c t . g e t M e t a d a t a ( M E T A _ K E Y , U s e r . p r o t o t y p e , " a c c e s s D a s h b o a r d " ) ;
c o n s o l e . l o g ( r o l e ) ; / / O u t p u t : ' a d m i n '
How NestJS Uses reflect-metadata Under the Hood
NestJS uses reflect-metadata extensively to implement its core features:
- Dependency Injection: NestJS stores type information to automatically inject dependencies
- Request Routing:
@Controller()and@Get()decorators store routing information - Guards & Interceptors: Decorators like
@UseGuards()attach metadata for request handling - Validation: Decorators mark which fields should be validated
3. Deep Dive: How NestJS Executes Decorators Internally
The NestJS Bootstrapping Process
When you start a NestJS application, this happens:
- The application calls
NestFactory.create(AppModule) - NestJS creates a dependency injection container
- It scans all modules starting from
AppModule - For each module, it reads metadata from decorators to identify:
- Controllers
- Providers (services, etc.)
- Imports/exports
- It builds a complete dependency graph
Step-by-Step Execution of a Decorator
Let's see how @Controller() works internally:
-
During application development, you decorate a class:
@ C o n t r o l l e r ( " u s e r s " ) e x p o r t c l a s s U s e r C o n t r o l l e r { / / C o n t r o l l e r m e t h o d s } -
When this code is compiled, TypeScript transforms it, calling the
Controllerfunction -
The
Controllerfunction (implemented by NestJS) stores metadata:/ / S i m p l i f i e d v e r s i o n o f w h a t N e s t J S d o e s i n t e r n a l l y f u n c t i o n C o n t r o l l e r ( p r e f i x ? : s t r i n g ) : C l a s s D e c o r a t o r { r e t u r n ( t a r g e t : o b j e c t ) = > { R e f l e c t . d e f i n e M e t a d a t a ( " p a t h " , p r e f i x | | " " , t a r g e t ) ; R e f l e c t . d e f i n e M e t a d a t a ( " _ _ i s C o n t r o l l e r " , t r u e , t a r g e t ) ; } ; } -
During application startup, NestJS uses its
DiscoveryServiceto scan modules:/ / C o n c e p t u a l e x a m p l e o f w h a t h a p p e n s i n t e r n a l l y c o n s t i s C o n t r o l l e r = R e f l e c t . g e t M e t a d a t a ( " _ _ i s C o n t r o l l e r " , U s e r C o n t r o l l e r ) ; i f ( i s C o n t r o l l e r ) { c o n s t p a t h = R e f l e c t . g e t M e t a d a t a ( " p a t h " , U s e r C o n t r o l l e r ) ; / / R e g i s t e r t h e c o n t r o l l e r w i t h t h e p a t h ' u s e r s ' } -
NestJS creates instances and sets up routing based on this metadata
This is a simplified explanation, but it shows how NestJS uses decorators and metadata to build its entire system.
Real-World Example: How @Injectable() Works
The @Injectable() decorator is core to NestJS's dependency injection. Here's how it works:
/ / Y o u r c o d e
@ I n j e c t a b l e ( )
e x p o r t c l a s s U s e r S e r v i c e {
c o n s t r u c t o r ( p r i v a t e d a t a b a s e S e r v i c e : D a t a b a s e S e r v i c e ) { }
}
/ / W h a t N e s t J S d o e s i n t e r n a l l y ( s i m p l i f i e d )
f u n c t i o n I n j e c t a b l e ( ) : C l a s s D e c o r a t o r {
r e t u r n ( t a r g e t : o b j e c t ) = > {
R e f l e c t . d e f i n e M e t a d a t a ( " _ _ i n j e c t a b l e " , t r u e , t a r g e t ) ;
/ / G e t p a r a m e t e r t y p e s f r o m T y p e S c r i p t
c o n s t t y p e s = R e f l e c t . g e t M e t a d a t a ( " d e s i g n : p a r a m t y p e s " , t a r g e t ) | | [ ] ;
/ / S t o r e d e p e n d e n c y t y p e s f o r l a t e r i n j e c t i o n
R e f l e c t . d e f i n e M e t a d a t a ( " _ _ p a r a m t y p e s " , t y p e s , t a r g e t ) ;
} ;
}
/ / D u r i n g a p p l i c a t i o n s t a r t u p , N e s t J S c r e a t e s i n s t a n c e s
c o n s t p a r a m T y p e s = R e f l e c t . g e t M e t a d a t a ( " _ _ p a r a m t y p e s " , U s e r S e r v i c e ) ;
/ / p a r a m T y p e s w o u l d b e [ D a t a b a s e S e r v i c e ]
/ / N e s t J S w o u l d t h e n :
/ / 1 . C r e a t e / f i n d a n i n s t a n c e o f D a t a b a s e S e r v i c e
/ / 2 . C r e a t e U s e r S e r v i c e p a s s i n g i n t h e D a t a b a s e S e r v i c e i n s t a n c e
c o n s t d a t a b a s e S e r v i c e = c o n t a i n e r . g e t ( D a t a b a s e S e r v i c e ) ;
c o n s t u s e r S e r v i c e = n e w U s e r S e r v i c e ( d a t a b a s e S e r v i c e ) ;
4. Custom Decorators in NestJS
Creating custom decorators in NestJS is surprisingly easy because NestJS provides utilities to simplify the process.
Example: Creating a @Permissions Decorator
i m p o r t { S e t M e t a d a t a } f r o m " @ n e s t j s / c o m m o n " ;
/ / N e s t J S p r o v i d e s S e t M e t a d a t a t o m a k e d e c o r a t o r c r e a t i o n e a s i e r
e x p o r t c o n s t P e r m i s s i o n s = ( . . . p e r m i s s i o n s : s t r i n g [ ] ) = > S e t M e t a d a t a ( " p e r m i s s i o n s " , p e r m i s s i o n s ) ;
Using the Decorator in a Controller
i m p o r t { C o n t r o l l e r , G e t } f r o m " @ n e s t j s / c o m m o n " ;
i m p o r t { P e r m i s s i o n s } f r o m " . / p e r m i s s i o n s . d e c o r a t o r " ;
@ C o n t r o l l e r ( " u s e r s " )
e x p o r t c l a s s U s e r C o n t r o l l e r {
@ G e t ( )
@ P e r m i s s i o n s ( " r e a d : u s e r s " , " w r i t e : u s e r s " )
g e t U s e r s ( ) {
r e t u r n " U s e r s L i s t " ;
}
}
Retrieving Metadata in a Guard
NestJS provides the Reflector class to simplify metadata retrieval:
i m p o r t { C a n A c t i v a t e , E x e c u t i o n C o n t e x t , I n j e c t a b l e } f r o m " @ n e s t j s / c o m m o n " ;
i m p o r t { R e f l e c t o r } f r o m " @ n e s t j s / c o r e " ;
@ I n j e c t a b l e ( )
e x p o r t c l a s s P e r m i s s i o n s G u a r d i m p l e m e n t s C a n A c t i v a t e {
c o n s t r u c t o r ( p r i v a t e r e f l e c t o r : R e f l e c t o r ) { }
c a n A c t i v a t e ( c o n t e x t : E x e c u t i o n C o n t e x t ) : b o o l e a n {
/ / G e t t h e p e r m i s s i o n s r e q u i r e d f o r t h i s e n d p o i n t
c o n s t r e q u i r e d P e r m i s s i o n s = t h i s . r e f l e c t o r . g e t < s t r i n g [ ] > ( " p e r m i s s i o n s " , c o n t e x t . g e t H a n d l e r ( ) ) ;
i f ( ! r e q u i r e d P e r m i s s i o n s ) {
r e t u r n t r u e ; / / N o p e r m i s s i o n s r e q u i r e d
}
c o n s t r e q u e s t = c o n t e x t . s w i t c h T o H t t p ( ) . g e t R e q u e s t ( ) ;
c o n s t u s e r = r e q u e s t . u s e r ;
/ / C h e c k i f u s e r h a s a l l r e q u i r e d p e r m i s s i o n s
r e t u r n r e q u i r e d P e r m i s s i o n s . e v e r y ( ( p e r m i s s i o n ) = > u s e r ? . p e r m i s s i o n s ? . i n c l u d e s ( p e r m i s s i o n ) ) ;
}
}
Advanced: Custom Parameter Decorators
NestJS also supports custom parameter decorators, which can extract and transform data from requests:
i m p o r t { c r e a t e P a r a m D e c o r a t o r , E x e c u t i o n C o n t e x t } f r o m " @ n e s t j s / c o m m o n " ;
/ / C r e a t e a d e c o r a t o r t h a t e x t r a c t s a s p e c i f i c u s e r p r o p e r t y
e x p o r t c o n s t U s e r P r o p e r t y = c r e a t e P a r a m D e c o r a t o r ( ( d a t a : s t r i n g , c t x : E x e c u t i o n C o n t e x t ) = > {
c o n s t r e q u e s t = c t x . s w i t c h T o H t t p ( ) . g e t R e q u e s t ( ) ;
c o n s t u s e r = r e q u e s t . u s e r ;
/ / d a t a i s t h e p r o p e r t y n a m e p a s s e d t o t h e d e c o r a t o r
r e t u r n d a t a ? u s e r ? . [ d a t a ] : u s e r ;
} ) ;
/ / U s a g e i n a c o n t r o l l e r
@ C o n t r o l l e r ( " u s e r s " )
e x p o r t c l a s s U s e r C o n t r o l l e r {
@ G e t ( " p r o f i l e " )
g e t P r o f i l e ( @ U s e r P r o p e r t y ( " i d " ) u s e r I d : s t r i n g ) {
r e t u r n ` U s e r I D : $ { u s e r I d } ` ;
}
}
Composing Multiple Decorators
One powerful feature of decorators is that you can combine them:
i m p o r t { a p p l y D e c o r a t o r s , G e t , U s e G u a r d s } f r o m " @ n e s t j s / c o m m o n " ;
i m p o r t { P e r m i s s i o n s G u a r d } f r o m " . / p e r m i s s i o n s . g u a r d " ;
i m p o r t { P e r m i s s i o n s } f r o m " . / p e r m i s s i o n s . d e c o r a t o r " ;
/ / C r e a t e a c o m b i n e d d e c o r a t o r
e x p o r t f u n c t i o n S e c u r e E n d p o i n t ( p a t h : s t r i n g , . . . p e r m i s s i o n s : s t r i n g [ ] ) {
r e t u r n a p p l y D e c o r a t o r s ( G e t ( p a t h ) , P e r m i s s i o n s ( . . . p e r m i s s i o n s ) , U s e G u a r d s ( P e r m i s s i o n s G u a r d ) ) ;
}
/ / U s a g e
@ C o n t r o l l e r ( " u s e r s " )
e x p o r t c l a s s U s e r C o n t r o l l e r {
@ S e c u r e E n d p o i n t ( " a d m i n " , " r e a d : u s e r s " , " m a n a g e : u s e r s " )
g e t A d m i n D a s h b o a r d ( ) {
r e t u r n " A d m i n D a s h b o a r d " ;
}
}
This pattern is exactly how NestJS creates many of its built-in decorators!
5. Debugging Decorators in NestJS
Decorators can sometimes be tricky to debug, so here are some tips:
Common Decorator Problems and Solutions
Problem: Metadata Not Being Applied
/ / T h i s m i g h t n o t w o r k a s e x p e c t e d
@ C o n t r o l l e r ( " u s e r s " )
c l a s s U s e r C o n t r o l l e r { }
Solution: Make sure you're exporting the class:
@ C o n t r o l l e r ( " u s e r s " )
e x p o r t c l a s s U s e r C o n t r o l l e r { }
Problem: Decorator Order Matters
/ / T h i s m i g h t p r o d u c e u n e x p e c t e d r e s u l t s
@ U s e G u a r d s ( A u t h G u a r d )
@ P e r m i s s i o n s ( ' a d m i n ' )
@ G e t ( )
m e t h o d ( ) { }
Solution: Remember that decorators are applied bottom-to-top, so reorder them:
@ G e t ( )
@ P e r m i s s i o n s ( ' a d m i n ' )
@ U s e G u a r d s ( A u t h G u a r d )
m e t h o d ( ) { }
Problem: Missing reflect-metadata
If you get errors about metadata, make sure:
/ / A t y o u r a p p l i c a t i o n e n t r y p o i n t
i m p o r t " r e f l e c t - m e t a d a t a " ;
Inspecting Metadata at Runtime
You can debug metadata by adding:
c o n s o l e . l o g ( R e f l e c t . g e t M e t a d a t a K e y s ( t a r g e t ) ) ;
c o n s o l e . l o g ( R e f l e c t . g e t M e t a d a t a ( " k e y " , t a r g e t ) ) ;
6. Performance Considerations with NestJS Decorators
While decorators are powerful, they do have performance implications:
Benchmarking Decorator Overhead
In my testing, I found that heavy use of decorators and metadata can add approximately 5-10% overhead to request processing times, though this varies widely based on usage.
Optimization Strategies
-
Cache Metadata Lookups: If you're repeatedly accessing the same metadata, store it in a variable
/ / I n s t e a d o f t h i s i n a h o t p a t h c o n s t p e r m i s s i o n s = t h i s . r e f l e c t o r . g e t ( ' p e r m i s s i o n s ' , h a n d l e r ) ; / / D o t h i s p r i v a t e r e a d o n l y h a n d l e r P e r m i s s i o n s M a p = n e w M a p ( ) ; g e t P e r m i s s i o n s ( h a n d l e r ) { i f ( ! t h i s . h a n d l e r P e r m i s s i o n s M a p . h a s ( h a n d l e r ) ) { c o n s t p e r m i s s i o n s = t h i s . r e f l e c t o r . g e t ( ' p e r m i s s i o n s ' , h a n d l e r ) ; t h i s . h a n d l e r P e r m i s s i o n s M a p . s e t ( h a n d l e r , p e r m i s s i o n s ) ; } r e t u r n t h i s . h a n d l e r P e r m i s s i o n s M a p . g e t ( h a n d l e r ) ; } -
Minimize Decorator Count: Don't overuse decorators; each one adds processing time
-
Lazy Evaluation: If your decorator performs expensive operations, do them lazily:
f u n c t i o n E x p e n s i v e O p e r a t i o n ( ) { l e t r e s u l t ; r e t u r n ( t a r g e t , k e y , d e s c r i p t o r ) = > { c o n s t o r i g i n a l = d e s c r i p t o r . v a l u e ; d e s c r i p t o r . v a l u e = f u n c t i o n ( . . . a r g s ) { / / O n l y c a l c u l a t e o n c e i f ( ! r e s u l t ) { r e s u l t = / * e x p e n s i v e c a l c u l a t i o n * / ; } r e t u r n o r i g i n a l . a p p l y ( t h i s , [ . . . a r g s , r e s u l t ] ) ; } ; } ; }
Conclusion
NestJS decorators simplify development by abstracting complex logic into reusable annotations. Understanding their internals provides better debugging, optimization, and customization capabilities.
- TypeScript decorators transform class behavior at runtime through compiler transformations
- NestJS enhances decorators using
reflect-metadatato implement its dependency injection system - Custom decorators allow developers to extend NestJS functionality efficiently
- Debugging and optimizing decorators requires understanding their execution flow
With this knowledge, you can now optimize, debug, and even build advanced decorators in NestJS like a pro! ๐