Slide 1

Slide 1 text

Alive, tipi sopravvissuti Luca Del Puppo Milano Frontend

Slide 2

Slide 2 text

1 → Reason Why? 2 → Common Mistakes 3 → strict mode 4 → any vs unknown Index Alive, tipi sopravvissuti Index 5 → Guards 6 → Validations 7 → Conclusion

Slide 3

Slide 3 text

Ego Slide Luca Del Puppo (aka Puppo) Full-Stack Developer in Flowing a Claranet Italian Company @puppo92 https://www.linkedin.com/in/lucadelpuppo/ [email protected]

Slide 4

Slide 4 text

Reason Why? 1

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Common Mistakes 2

Slide 9

Slide 9 text

➔ To think that typescript keeps always the code type-safe ➔ To forget that types disappears at runtime ➔ To use any if checking types is too hard or boring ➔ Don't know or forget the basics of js ➔ Don’t put a validation layer between the application and the externals Common Mistakes Alive, tipi sopravvissuti Common Mistakes

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

➔ Use types, interfaces or class to describe your object ➔ Describe at the best your types, use undefined and null if they are necessary ➔ Create the right signature of your methods (don’t forget the result type) ➔ Prefer immutable types (readonly, ReadOnly, ReadOnlyArray) The basic Alive, tipi sopravvissuti

Slide 12

Slide 12 text

strict mode 3

Slide 13

Slide 13 text

➔ It forces us to use the type-checker as a friend ➔ It prevents wrong checks or wrong accesses to the objects ➔ It forces us to write the code with the correct signatures ➔ It helps to find the errors at compile time instead of runtime Strict mode Alive, tipi sopravvissuti Strict mode

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

any vs unknown 4

Slide 16

Slide 16 text

any

Slide 17

Slide 17 text

unknow

Slide 18

Slide 18 text

let myAny: any = true myAny.trim().reverse().split(','); let myAnyNumeric: number = myAny; const sum = myAnyNumeric + 3; let myUnknown: unknown = true; myUnknown.trim(); // Property 'trim' does not exist on type 'unknown'.ts(2339) let myAnyNumeric: number = myUnknown; // Type 'unknown' is not assignable to type 'number'.ts(2322) if (typeof myUnknown === 'string') myUnknown.trim(); if (typeof myUnknown === "number") { let myAnyNumeric: number = myUnknown; const sum = myAnyNumeric + 3; } Any vs Unknown

Slide 19

Slide 19 text

➔ eslint-config-airbnb-typescript ➔ @typescript-eslint/recommended Eslint Alive, tipi sopravvissuti Strict Mode

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Guards 5

Slide 22

Slide 22 text

“Type guards are functions used to check if an object respect a specific type” Their form is Type Guard Alive, tipi sopravvissuti Guards declare function name(data: unknow): data is Type;

Slide 23

Slide 23 text

“Assert functions are functions used to check if an object respect a specific type otherwise they throw an error” Their form is Assert Functions Alive, tipi sopravvissuti Guards declare function name(data: unknow): asserts data is Type;

Slide 24

Slide 24 text

type Circle = { type: 'circle'; radius: number; } type Square = { type: 'square'; size: number; } type Shape = Circle | Square; function isCircle(shape: Shape): shape is Circle { return shape.type === 'circle'; } function isSquare(shape: Shape): shape is Square { return shape.type === 'square'; } type Circle = { type: 'circle'; radius: number; } type Square = { type: 'square'; size: number; } type Shape = Circle | Square; function ensureCircle(shape: Shape): asserts shape is Circle { if (shape.type !== 'circle') throw new Error('Not a circle'); } function ensureSquare(shape: Shape): asserts shape is Square { if (shape.type !== 'square') throw new Error('Not a square'); } Type Guards vs Assert Functions

Slide 25

Slide 25 text

Validations 6

Slide 26

Slide 26 text

“In this case using the Validation word I want to focus you to the layer between your application and the external” - Data from API - Data from socket - All the data received from the external of your application Validation Alive, tipi sopravvissuti Validation

Slide 27

Slide 27 text

- Prevent wrong types at runtime - Keep your code safer like during the development mode - Help you to find quickly if the APIs are changed - Prevent strange mistakes - You are sure that the API results are exactly what you expected Goal Alive, tipi sopravvissuti Validation

Slide 28

Slide 28 text

➔ Zod https://github.com/colinhacks/zod ➔ @sinclair/typebox https://github.com/sinclairzx81/typebox ➔ jsonschema https://github.com/tdegrunt/jsonschema ➔ io-ts https://github.com/gcanti/io-ts Validation library Alive, tipi sopravvissuti Validation

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Conclusion

Slide 31

Slide 31 text

code Alive Types code in action https://puppo.github.io/alive-types/ dev.to series https://dev.to/this-is-learning/series/11213

Slide 32

Slide 32 text

Thanks! That’s All! Luca Del Puppo @puppo92 https://www.linkedin.com/in/lucadelpuppo/ [email protected]

Slide 33

Slide 33 text

Sito https://t.co/7Z4wXMapva Coupon (-15%) LucaCarbonara22 NgRome - 02 Ottobre 2022