Slide 1

Slide 1 text

πŸͺ†πŸͺ†πŸͺ†πŸͺ†πŸͺ†πŸͺ†πŸͺ† Decorators in TypeScript 5.0 Everything you need to know!

Slide 2

Slide 2 text

πŸ–– Hello! @spontoreau Sylvain Pontoreau πŸŽͺ Co-organizer πŸ“• Book writer Microsoft 󰳕 Work with

Slide 3

Slide 3 text

❓ Why I am talking about this topic ?

Slide 4

Slide 4 text

πŸͺ† What's a decorator? @Decorator A pattern that allows to add dynamically a behavior... On an element of a class… With an annotation expression:

Slide 5

Slide 5 text

🏺 How it started ? Added as experimental in version 1.5 (2016) Used in Angular 2.0

Slide 6

Slide 6 text

πŸ”Ž Experimental Decorator Applies to: - Classes - Methods - Fields - Accessors - Parameters Requirements (tsconfig): - --emitDecoratorMetadata - --experimentalDecorators Covered by TypeScript: - Types (example: MethodDecorator) - Decorator Factory - Metadata (with reflect-metadata library)

Slide 7

Slide 7 text

󰳕󰳕󰳕󰳕󰳕󰳕󰳕 πŸͺ† Example

Slide 8

Slide 8 text

πŸ“¦ Which library uses them? Some examples…

Slide 9

Slide 9 text

πŸ“œ ECMAScript proposal https://github.com/tc39/proposal-decorators

Slide 10

Slide 10 text

βŒ› Few years later

Slide 11

Slide 11 text

🏷 Planned in TypeScript 4.9… Finally available in 5.0 !

Slide 12

Slide 12 text

πŸ”Ž ECMAScript Decorator Applies to: - βœ… Classes - βœ… Methods - βœ… Fields - βœ… Accessors (get, set, auto) - ❌ Parameters Requirements (tsconfig): - --emitDecoratorMetadata - --experimentalDecorators Covered by TypeScript: - βœ… Types (context only) - βœ… Decorator Factory - ❌ Metadata Context data: - kind (all) - name (all) - addInitializer (all - static (all except classes) - private (all except classes) - access (all except classes)

Slide 13

Slide 13 text

πŸ”Ž ECMAScript Decorator ⚠ Not compatible with experimental decorators type Decorator = ( target: TTarget, context: TContext ) => TReturn | void In typing, an ECMAScript decorator is :

Slide 14

Slide 14 text

πŸ”Ž ECMAScript Decorator Classes type Target = new (...args: any[]) => any; type Context = ClassDecoratorContext; type Return = Target; Methods type Target = ( this: any, ...args: any[] ) => any | void; type Context = ClassMethodDecoratorContext; type Return = Target; Fields type Target = undefined; type Context = ClassFieldDecoratorContext; type Return = (this: any, value: any) => any;

Slide 15

Slide 15 text

πŸ”Ž ECMAScript Decorator Getter type Target = (this: any) => any; type Context = ClassGetterDecoratorContext; type Return = Target; Setter type Target = (this: any, value: any) => void; type Context = ClassSetterDecoratorContext; type Return = Target Auto accessor type Target = ClassAccessorDecoratorTarget; type Context = ClassAccessorDecoratorContext; type Return = ClassAccessorDecoratorResult;

Slide 16

Slide 16 text

󰳕󰳕󰳕󰳕󰳕󰳕󰳕 πŸͺ† Example

Slide 17

Slide 17 text

❓❓❓❓❓❓❓ πŸͺ† Questions?

Slide 18

Slide 18 text

πŸ™πŸ™πŸ™πŸ™πŸ™πŸ™πŸ™ πŸͺ† Thanks!