Advanced Typescript for people in hurry

Bartosz Szewczyk

>6 @ Codete

2022

Me, my bike and my dress code

Me and bike

Game that I'm making in my free time

(not released yet) My game in progress

Overview

  1. Type Guards
  2. Strict Mode
  3. Enums
  4. Advanced types tips
  5. Breaking Typescript
  6. Useful links & materials

Typescript has become more powerful

What can you build with types alone?

How about a sorting algorithm?

Typescript playground

1. Type guards

How to determine a specific type

Type guards #1

An interface with optional values

Does it work

Solution #1

Typescript playground

Solution #2

Typescript playground

Type guards #2

A type union

Does it work

Solution #1

Typescript playground
I Pulled a Sneaky on Ya

Solution #2

Typescript playground

Type guards #3

An unknown type

When do we encounter unknown

Does it work

Solution

Typescript playground

Type guards #4

Discriminated union types

Typescript playground

Bonus feature

for discriminated unions
since 4.6

Typescript playground
Did you enjoy that? I did :D

2. Strict mode

2. Strict mode

  1. strictNullChecks
  2. strictBindCallApply
  3. strictFunctionTypes
  4. strictPropertyInitialization
  5. useUnknownInCatchVariables

2. Strict mode

  1. strictNullChecks
  2. strictBindCallApply
  3. strictFunctionTypes
  4. strictPropertyInitialization
  5. useUnknownInCatchVariables

strict null checks

TS playground

strict function type

no bivariant or contravariant shenanigans

strict function type

🙈 TS playground

Incremental strict mode

How to apply it only to part of application 🤔

Files hierarchy

3. Enums in typescript

How can we create an enum in typescript

Enums

What's the difference

Can we mix them?

Ts Playground

4. Advanced Types tips

  • Type conditionals
  • Mapped Types
  • Infer Types

Type conditionals #1

Type conditionals #1

Proof

Type conditionals #2

Proof

Type conditionals #3

Proof

Type conditionals #4

IsEqual #1

Proof

Type conditionals #4

IsEqual #2

Proof

Type conditionals #4

IsEqual #3

Proof

Mapped types #1

Proof

Mapped types + conditional types

Proof

Mapped types on arrays

Proof

Mapped types on arrays #2

Proof

Infer Types #1

Infer Types #2

Proof

Infer Types #2

How to fix it?

Proof

Conditional + Mapped + Infer-Types

Solution

Proof
Head Explode Gif

5. Breaking typescript

Example #1

Methods strict check

Typescript playground

Example #1.1

Methods strict check

With createNameGetter

Typescript playground

Example #2

Strict function type with void return

Typescript playground

Example #3

TS depend on duck-typing just like JS

Proof

6. Useful links

Like what you saw?

Want to become a Typescript Pro?

Say no more!

Useful utilities libraries

  1. SimplyTyped
  2. utility-types
  3. ts-toolbelt

type challenges

Repository link

Exiting upcoming features

Instantiation Expressions

Before:

Instantiation Expressions

After TS 4.7

Thank you

my face

Bartosz Szewczyk

👨🏼‍💻 Tech Lead @ Codete

Thank you gif