that never crash build frontend applications with fp-ts ... · typed superset of javascript...

54
1 Build frontend applications that never crash with fp-ts ecosystem AntonSutarmin

Upload: others

Post on 20-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

1

Build frontend applications that never crash with fp-ts ecosystem

AntonSutarmin

Page 2: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

2

Truly reliable frontend applications with fp-ts ecosystem

AntonSutarmin

Page 3: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

3

Page 4: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

4

Page 5: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

5

The quality of being able to be trusted to do what somebody wants or needs

Reliability

Page 6: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

6

Reliability Edge case coverage

No runtime crashes

Compliance with tech specs

Page 7: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

7

How to achieve better reliability?

Static types

Functional paradigm

Runtime type safety

Page 8: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

8

How to achieve better reliability?

Static types

Functional paradigm

Runtime type safety

Page 9: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

Static types

9

Dynamic types

● Faster initial development

● Better code reusability and

expressiveness

● Easier maintenance

● Self-documented code

Page 10: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

10

https://twitter.com/01k/status/1067788059989684224

Page 11: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

Typescript

11

● Very expressive type system

● Typed superset of JavaScript

● Compiles to plain JS

● Could be used in very different ways

Page 12: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

strictNullChecks: false

12

● null/undefined is a part of every type

● Works like in most modern programming languages

Page 13: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

strictNullChecks: false

13

● null/undefined is a part of every type

● Works like in most modern programming languages

Page 14: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

strictNullChecks: true

14

Page 15: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

strictNullChecks: true

15

Page 16: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

strictNullChecks: true

16

Works without deceiving a programmer

Page 17: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

TS: gotchas

17

Array indexing

Page 18: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

18

TS: gotchas

Array indexing

any type

Page 19: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

19

TS: gotchas

Array indexing

any type

Page 20: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

Static types: takeouts

20

Use responsibly

Don’t try to trick

Page 21: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

21

How to achieve better reliability?

Static types

Functional paradigm

Runtime type safety

Page 22: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

22

Functional paradigm

Immutable data

Pure functions

Page 23: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

23

Functional paradigm

Immutable data

Pure functions

Page 24: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

24

goto statement

Page 25: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

25

throw is new goto

try {

} catch (e) { /* good enough? */}

Page 26: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

26

Error | UserFP-way to pass errors

Page 27: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

27

Either<Error, User>FP-way to pass errors

Page 28: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

28

Either monad

Page 29: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

29

https://github.com/gcanti/fp-ts

Page 30: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

30

fp-ts

● Library for FP in TS

● Collection of data types and utilities

● Higher Kinded Types in Typescript!

Page 31: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

31

fp-ts/lib/Either

● Can be used for error handling

Page 32: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

32

fp-ts/lib/Either

● Can be used for error handling

● Pipeable API

Page 33: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

33

fp-ts/lib/Either

● Can be used for error handling

● Pipeable API

Page 34: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

34

fp-ts/lib/*

● Pipeable API

● Option

● Task

● Reader

● Writer

● IO

● TaskEither

● NonEmptyArray

● State

● Store

Page 35: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

35

fp-ts/lib/Either

● Can be used for error handling

● Pipeable API

Page 36: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

36

fp-ts/lib/Either

● Can be used for error handling

● Pipeable API

Page 37: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

37

fp-ts/lib/Either

● Can be used for error handling

● Pipeable API

● No runtime exceptions at all

Page 38: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

38

fp-ts/lib/Either

● Can be used for error handling

● Pipeable API

● No runtime exceptions at all

Page 39: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

39

fp-ts/lib/Either

● Can be used for error handling

● Pipeable API

● No runtime exceptions at all

Page 40: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

40

fp-ts/lib/Either

● Can be used for error handling

● Pipeable API

● No runtime exceptions at all

Page 41: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

41

fp-ts/lib/Either

● Can be used for error handling

● Pipeable API

● No runtime exceptions at all

Page 42: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

42

How to achieve better reliability?

Static types

Functional paradigm

Runtime type safety

Page 43: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

43

Runtime validation

Page 44: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

44

Runtime validation

Page 45: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

45

io-ts

● Part of fp-ts ecosystem

● Runtime type validation

function safeParseUser(str: string): E.Either<t.Errors, User> {

const parsedUser = JSON.parse(str);

return (parsedUser);

}

Page 46: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

46

io-ts: basic types

function safeParseUser(str: string): E.Either<t.Errors, User> {

const parsedUser = JSON.parse(str);

return userCodec.decode(parsedUser);

}

Page 47: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

47

io-ts: composability

userCodec

Page 48: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

48

io-ts: use cases

● http-requests

Page 49: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

49

io-ts: use cases

● http-requests

● local storage data

Page 50: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

50

Static types

Functional paradigm

Runtime type safety

Wearesafe!

Page 51: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

51

And there is a lot more!

● fp-ts-routing

● newtype-ts

● ...

Page 52: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly
Page 53: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

@AntonSutarmin

twitter.com/devexperts

facebook.com/devexperts github.com/devexperts

Stay tuned!

linkedin.com/company/devexperts

Page 54: that never crash Build frontend applications with fp-ts ... · Typed superset of JavaScript Compiles to plain JS Could be used in very different ways. strictNullChecks: ... Use responsibly

Questions?

@AntonSutarmin