All Aboard the Type Train All Aboard the Type Train Kadi Kraman - - PowerPoint PPT Presentation

all aboard the type train all aboard the type train
SMART_READER_LITE
LIVE PREVIEW

All Aboard the Type Train All Aboard the Type Train Kadi Kraman - - PowerPoint PPT Presentation

All Aboard the Type Train All Aboard the Type Train Kadi Kraman Kadi Kraman @kadikraman Formidable Why this talk? Why this talk? Why add types to JavaScript? Why add types to JavaScript? Should you use Flow or TypeScript? Should you use


slide-1
SLIDE 1

All Aboard the Type Train All Aboard the Type Train

Kadi Kraman Kadi Kraman

@kadikraman Formidable

slide-2
SLIDE 2

Why this talk? Why this talk?

slide-3
SLIDE 3

Why add types to JavaScript? Why add types to JavaScript? Should you use Flow or TypeScript? Should you use Flow or TypeScript?

... or something else entirely!

slide-4
SLIDE 4

All programming languages All programming languages have a type system have a type system

The difference is The difference is when when the type- the type- checking is done checking is done

slide-5
SLIDE 5

Strong vs Weak Strong vs Weak

Languages are often colloquially referred to as strongly typed or weakly typed. There is no universally accepted definition

  • f what these terms mean.

♀ ♀

slide-6
SLIDE 6

Static vs Dynamic Static vs Dynamic

JavaScript is a language Dynamically Typed Types are checked before run-time Types are checked at run-time, during execution

Static (e.g. Go, C#, Haskell) Static (e.g. Go, C#, Haskell) Dynamic (Python, Lua, Objective C) Dynamic (Python, Lua, Objective C)

slide-7
SLIDE 7

Static typing Static typing

Pros Pros Cons Cons

More errors found earlier in development Fewer errors at run-time and shipped code No need to write tests for "type-correctness" Verbose type declarations Complex error messages Excessive boilerplate (not JavaScript)

slide-8
SLIDE 8

Dynamic typing Dynamic typing

Pros Pros Cons Cons

Implicit polymorphism (the ability to write a single function that handles many data-types) Reduces clutter and repetition in code More errors detected during run time and in shipped code Need to write tests for type correctness (e.g. JavaScript)

slide-9
SLIDE 9

Additional type-checking Additional type-checking will not make code bug-free will not make code bug-free

It is not a replacement for testing your code It only helps reduce type errors

DISCLAIMER! DISCLAIMER!

slide-10
SLIDE 10

How to make JavaScript How to make JavaScript more type-safe? more type-safe?

Statically typed language that compiles to JavaScript Static code analysis

1. 1. 2. 2.

slide-11
SLIDE 11

Flow - static type checker Flow - static type checker

Static code analysis

1. 1.

Infers type information from existing code You can choose to enforce types (Atom, with Flow plugin) Facebook 2014

slide-12
SLIDE 12

TypeScript - a superset of TypeScript - a superset of JavaScript JavaScript

2.

  • 2. Statically typed language that compiles to JavaScript

Infers type information from existing code You can choose to enforce types (VSCode, with TypeScript plugin) Microsoft 2012

slide-13
SLIDE 13

"Opt in" by adding a flow declaration at the top of the file

Adding to an Adding to an existing codebase existing codebase

Flow Flow TypeScript TypeScript

TS is a superset of JS (so any valid JS file is also a valid TS file) But you do have to change the file extension to .ts

slide-14
SLIDE 14

Configuration Configuration

Flow Flow TypeScript TypeScript

slide-15
SLIDE 15

Performance Performance

Flow Flow TypeScript TypeScript

Slow to recompile on large projects

  • Usually fast, but

notoriously unstable

slide-16
SLIDE 16

Community Community

Flow Flow TypeScript TypeScript

(by Facebook, 2014) (by Microsoft, 2012)

slide-17
SLIDE 17

Typing Node Modules Typing Node Modules

Flow Flow TypeScript TypeScript

slide-18
SLIDE 18

So why are a lot of projects So why are a lot of projects moving to TypeScript? moving to TypeScript?

Larger community Faster release cycle More reliable The features that made flow "better" have been implemented in TypeScript

slide-19
SLIDE 19

What you should know before jumping What you should know before jumping

  • n the TypeScript "Type Train"
  • n the TypeScript "Type Train"

For best results, use VSCode Be prepared for a lot of Object Oriented influence tslint (the TypeSctipt linter) will be deprecated in 2019 So use typescript-eslint TypeScript is a compiled language, not a static type-checker - if you have type errors in your code, it will not compile

slide-20
SLIDE 20

Elm (2012 by Evan Czaplicki) Compiled, statically typed, type declarations are optional, purely functional. ReasonML (2016 by Jordan Walke at Facebook) Transpiles to OCaml which compiles to JS, statically typed

"Why not just use a proper statically typed "Why not just use a proper statically typed language?" language?"

slide-21
SLIDE 21

Thank You Thank You

All Aboard the Type Train All Aboard the Type Train

by Kadi Kraman (@kadikraman)