All Aboard the Type Train

Kadi Kraman

@kadikraman

Formidable

Why this talk?

Why add types to JavaScript?

Should you use Flow or TypeScript?

... or something else entirely!

All programming languages have a type system

The difference is when the type-checking is done

Strong vs Weak

Languages are often colloquially referred to as strongly typed or weakly typed.

There is no universally accepted definition of what these terms mean.

🤦‍♀️

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)

Dynamic (Python, Lua, Objective C)

Static typing

Pros

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)

Dynamic typing

Pros

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)

Additional type-checking will not make code bug-free

It is not a replacement for testing your code

It only helps reduce type errors

DISCLAIMER!

How to make JavaScript more type-safe?

Statically typed language that compiles to JavaScript

Static code analysis

1.

2.

Flow - static type checker

Static code analysis

1.

Infers type information from existing code

You can choose to enforce types

(Atom, with Flow plugin)

Facebook 2014

TypeScript - a superset of JavaScript

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

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

Adding to an existing codebase

Flow

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

Configuration

Flow

TypeScript

Performance

Flow

TypeScript

Slow to recompile on large projects

😱

Usually fast, but notoriously unstable

Community

Flow

TypeScript

(by Facebook, 2014)

(by Microsoft, 2012)

Typing Node Modules

Flow

TypeScript

So why are a lot of projects moving to TypeScript?

Larger community

Faster release cycle

More reliable

The features that made flow "better" have been implemented in TypeScript

What you should know before jumping on 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

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 language?"

Thank You 🤗

All Aboard the Type Train

by Kadi Kraman (@kadikraman)

All Aboard the Type Train

By Kadi Kraman

All Aboard the Type Train

JavaScript is a language of add-ons, and one of its more recent trends is typing. There are obvious benefits to typing, but what are the drawbacks? Should you choose Flow or TypeScript? Why use types at all? And why not just go straight to Reason?

  • 38
Loading comments...

More from Kadi Kraman