Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
KAMMYSLIWIEC DIVING INTO TYPESCRIPT DESIGN METADATA WITH NESTJS
Slide 2
Slide 2 text
KAMMYSLIWIEC KAMIL MYSLIWIEC CREATOR OF NESTJS | CO-FOUNDER OF TRILON.IO GOOGLE DEVELOPER EXPERT @KAMMYSLIWIEC
Slide 3
Slide 3 text
KAMIL MYŚLIWIEC SOFTWARE ENGINEER @KAMMYSLIWIEC
Slide 4
Slide 4 text
KAMMYSLIWIEC Adonis Express Koa Nest Fastify
Slide 5
Slide 5 text
NESTJS WWW.NESTJS.COM @NESTFRAMEWORK KAMMYSLIWIEC
Slide 6
Slide 6 text
DESIGN-TIME METADATA
Slide 7
Slide 7 text
KAMMYSLIWIEC PARAMETER TYPE METADATA
Slide 8
Slide 8 text
DEPENDENCY INJECTION
Slide 9
Slide 9 text
KAMMYSLIWIEC @Injectable() class CatsService { private httpService = new HttpService(); private logger = new Logger(); }
Slide 10
Slide 10 text
KAMMYSLIWIEC @Injectable() class CatsService { private httpService = new HttpService(); private logger = new Logger(); }
Slide 11
Slide 11 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private httpService: HttpService, private logger: Logger, ) {} }
Slide 12
Slide 12 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private httpService: HttpService, private logger: Logger, ) {} }
Slide 13
Slide 13 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private httpService: HttpService, private logger: Logger, ) {} }
Slide 14
Slide 14 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private httpService: HttpService, private logger: Logger, ) {} }
Slide 15
Slide 15 text
KAMMYSLIWIEC const Injectable: ClassDecorator = (target: Object) => {};
Slide 16
Slide 16 text
KAMMYSLIWIEC COMPILER METADATA
Slide 17
Slide 17 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private httpService: HttpService, private logger: Logger, ) {} }
Slide 18
Slide 18 text
KAMMYSLIWIEC CatsService = __decorate( [__metadata('design:paramtypes', [HttpService, Logger])], CatsService, );
Slide 19
Slide 19 text
KAMMYSLIWIEC CatsService = __decorate( [__metadata('design:paramtypes', [HttpService, Logger])], CatsService, );
Slide 20
Slide 20 text
KAMMYSLIWIEC CatsService = __decorate( [__metadata('design:paramtypes', [HttpService, Logger])], CatsService, );
Slide 21
Slide 21 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private httpService: HttpService, private logger: Logger, ) {} }
Slide 22
Slide 22 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private httpService: HttpService, private logger: Logger, ) {} }
Slide 23
Slide 23 text
REFLECTION API
Slide 24
Slide 24 text
KAMMYSLIWIEC Reflect.getMetadata('design:paramtypes', CatsService);
Slide 25
Slide 25 text
KAMMYSLIWIEC Reflect.getMetadata('design:paramtypes', CatsService);
Slide 26
Slide 26 text
KAMMYSLIWIEC Reflect.getMetadata('design:paramtypes', CatsService);
Slide 27
Slide 27 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:paramtypes', CatsService, ); // metadata = [HttpService, Logger]
Slide 28
Slide 28 text
PIPES
Slide 29
Slide 29 text
KAMMYSLIWIEC @Injectable() class ExponentialStrengthPipe implements PipeTransform { transform( value: number, metadata: ArgumentMetadata, ): number {} }
Slide 30
Slide 30 text
KAMMYSLIWIEC @Injectable() class ExponentialStrengthPipe implements PipeTransform { transform( value: number, metadata: ArgumentMetadata, ): number {} }
Slide 31
Slide 31 text
KAMMYSLIWIEC @Injectable() class ExponentialStrengthPipe implements PipeTransform { transform( value: number, metadata: ArgumentMetadata, ): number {} }
Slide 32
Slide 32 text
KAMMYSLIWIEC @Injectable() class ExponentialStrengthPipe implements PipeTransform { transform( value: number, metadata: ArgumentMetadata, ): number {} }
Slide 33
Slide 33 text
KAMMYSLIWIEC interface ArgumentMetadata { type: 'body' | 'query' | 'param' | 'custom'; metatype?: Type; data?: string; }
Slide 34
Slide 34 text
KAMMYSLIWIEC @Post() async create( @Body() createDto: CreateRecipeDto, ): Promise { return 'This action adds a new recipe'; } TYPE METATYPE DATA
Slide 35
Slide 35 text
KAMMYSLIWIEC @Post() async create( @Body() createDto: CreateRecipeDto, ): Promise { return 'This action adds a new recipe'; } TYPE METATYPE DATA
Slide 36
Slide 36 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:paramtypes', new HostClass(), 'create' ); // metadata = [CreateRecipeDto]
Slide 37
Slide 37 text
KAMMYSLIWIEC REFLECTION API CHALLENGES
Slide 38
Slide 38 text
KAMMYSLIWIEC INTERFACES
Slide 39
Slide 39 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private httpService: HttpService, private logger: Logger, ) {} }
Slide 40
Slide 40 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private httpService: IHttpService, private logger: Logger, ) {} }
Slide 41
Slide 41 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:paramtypes', CatsService, ); // metadata = [Object, Logger] // IHttpService === Object
Slide 42
Slide 42 text
KAMMYSLIWIEC POTENTIAL SOLUTION
Slide 43
Slide 43 text
KAMMYSLIWIEC GENERICS
Slide 44
Slide 44 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private httpService: HttpService, private logger: Logger, ) {} }
Slide 45
Slide 45 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private httpService: HttpService, private catsRepository: Repository, ) {} }
Slide 46
Slide 46 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:paramtypes', CatsService, ); // metadata = [HttpService, Repository] // what about Cat class?
Slide 47
Slide 47 text
KAMMYSLIWIEC POTENTIAL SOLUTION
Slide 48
Slide 48 text
KAMMYSLIWIEC CIRCULAR DEPENDENCIES
Slide 49
Slide 49 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( private dogsService: DogsService, ) {} }
Slide 50
Slide 50 text
KAMMYSLIWIEC @Injectable() class DogsService { constructor( private catsService: CatsService, ) {} }
Slide 51
Slide 51 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:paramtypes', CatsService, ); // metadata = [undefined] // DogsService === undefined
Slide 52
Slide 52 text
KAMMYSLIWIEC POTENTIAL SOLUTION
Slide 53
Slide 53 text
KAMMYSLIWIEC @Injectable() class CatsService { constructor( @Inject(forwardRef(() => DogsService)) private dogsService: DogsService, ) {} }
Slide 54
Slide 54 text
KAMMYSLIWIEC function forwardRef(forwardRefFn: ForwardRefFn): Type { (forwardRefFn).__forward_ref__ = forwardRef; ... }
Slide 55
Slide 55 text
KAMMYSLIWIEC function forwardRef(forwardRefFn: ForwardRefFn): Type { (forwardRefFn).__forward_ref__ = forwardRef; ... }
Slide 56
Slide 56 text
KAMMYSLIWIEC TYPE METADATA
Slide 57
Slide 57 text
KAMMYSLIWIEC export class User { email: string; password: string; }
Slide 58
Slide 58 text
KAMMYSLIWIEC export class User { email: string; password: string; }
Slide 59
Slide 59 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:type', new User(), 'email' ); // metadata = undefined
Slide 60
Slide 60 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:type', X ‘design:paramtype’ new User(), 'email' ); // metadata = undefined
Slide 61
Slide 61 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:type', new User(), 'email' ); // metadata = undefined
Slide 62
Slide 62 text
KAMMYSLIWIEC export class User { email: string; password: string; }
Slide 63
Slide 63 text
KAMMYSLIWIEC export class User { @Property email: string; @Property password: string; }
Slide 64
Slide 64 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:type', new User(), 'email' ); // metadata = String
Slide 65
Slide 65 text
KAMMYSLIWIEC WHAT’S THE POINT?
Slide 66
Slide 66 text
No content
Slide 67
Slide 67 text
KAMMYSLIWIEC export class User { @Property email: string; @Property password: string; }
Slide 68
Slide 68 text
KAMMYSLIWIEC User: type: object properties: email: type: string password: type: string
Slide 69
Slide 69 text
KAMMYSLIWIEC LIMITATIONS
Slide 70
Slide 70 text
KAMMYSLIWIEC export class User { @Property email: string; @Property password: string; }
Slide 71
Slide 71 text
KAMMYSLIWIEC export class User { @Property email: string; @Property password: string; @Property tags: string[]; }
Slide 72
Slide 72 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:type', new User(), 'tags' ); // metadata = Array
Slide 73
Slide 73 text
KAMMYSLIWIEC export class User { @Property email: string; @Property password: string; @Property tags: string[]; }
Slide 74
Slide 74 text
KAMMYSLIWIEC export class User { @Property email: string; @Property password: string; @Property tags: Array; }
Slide 75
Slide 75 text
KAMMYSLIWIEC POTENTIAL SOLUTION
Slide 76
Slide 76 text
KAMMYSLIWIEC TYPESCRIPT PLUGINS
Slide 77
Slide 77 text
KAMMYSLIWIEC const emitResult = program.emit( undefined, undefined, undefined, undefined, { before: [genericsMetadataPlugin], after: [], }, );
Slide 78
Slide 78 text
KAMMYSLIWIEC const emitResult = program.emit( undefined, undefined, undefined, undefined, { before: [genericsMetadataPlugin], after: [], }, );
Slide 79
Slide 79 text
KAMMYSLIWIEC RETURN TYPE METADATA
Slide 80
Slide 80 text
KAMMYSLIWIEC @Post() create(): string { return 'This action adds a new recipe'; }
Slide 81
Slide 81 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( ‘design:returntype', new HostClass(), 'create' ); // metadata = String
Slide 82
Slide 82 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( ‘design:returntype', X ‘design:type’ new HostClass(), 'create' ); // metadata = String
Slide 83
Slide 83 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:returntype', new HostClass(), 'create' ); // metadata = String
Slide 84
Slide 84 text
KAMMYSLIWIEC WHY IS IT USEFUL?
Slide 85
Slide 85 text
KAMMYSLIWIEC LIMITATIONS
Slide 86
Slide 86 text
KAMMYSLIWIEC @Post() async create(): Promise { return 'This action adds a new recipe'; }
Slide 87
Slide 87 text
KAMMYSLIWIEC const metadata = Reflect.getMetadata( 'design:returntype', new HostClass(), 'create' ); // metadata = Promise // what about “String”?
Slide 88
Slide 88 text
RECAP
Slide 89
Slide 89 text
KAMMYSLIWIEC OPEN SOURCE
Slide 90
Slide 90 text
KAMMYSLIWIEC KAMMYSLIWIEC WWW.TRILON.IO
Slide 91
Slide 91 text
KAMMYSLIWIEC OPEN SOURCE
Slide 92
Slide 92 text
KAMMYSLIWIEC KAMMYSLIWIEC THANK YOU @KAMMYSLIWIEC