+ All Categories
Home > Engineering > Getting started with Flow

Getting started with Flow

Date post: 09-Feb-2017
Category:
Upload: charlie-dowler
View: 382 times
Download: 1 times
Share this document with a friend
20
Getting started with Flow @charliedowler
Transcript
Page 1: Getting started with Flow

Getting started with Flow

@charliedowler

Page 2: Getting started with Flow

Intro

• Junior Software Engineer @ Hindsight Software

• Working with Backbone, React, ImmutableJS

Page 3: Getting started with Flow

Typed Javascript

• Elm

• Typescript (Microsoft)

• Dart (Google)

• Flow (Facebook)

Page 4: Getting started with Flow

What is ?

• “Improvement on Typescript”

• Type checking for your javascript applications

• Catch compile time errors

• Can be introduced with small gradual changes

Page 5: Getting started with Flow

Why write typed JS using Flow?

• Dynamic + Static typed code can mix

• Scalable

• Maintainable

• Small learning curve

• Compatible with existing vanilla JS and JSX

Page 6: Getting started with Flow

Unsafe code

function size(str) { return str.length;}

size(null);

// Property length not found on null

Page 7: Getting started with Flow

Safe codefunction size(str) { if (!str) {

return 0; } return

str.length;}size(null);size(10);

Page 8: Getting started with Flow

Types• number

• string

• boolean

• void

• mixed

• any

Page 9: Getting started with Flow

Variables

var $name: string = “Charlie”;

Page 10: Getting started with Flow

Functions

function getName(): string { return $name;}

function setName(name: string): void { $name = name;}

Page 11: Getting started with Flow

Classesclass StateMachine { function log(msg: mixed): void; function transition(nextState: string): void;}

class CustomMachine extends StateMachine { constructor() {

this.log(“instance of CustomMachine has been created”); this.transition(StateConstants.READY);}

}

Page 12: Getting started with Flow

Objectstype User = { id: number, verified: boolean, email: string, firstName: string, lastName: string}

function updateUser(updatedUser: User) { ...}

Page 13: Getting started with Flow

More typesvar scene: GameScene = new GameScene()

var scene2: typeof scene = GameScene.create()

var name: ?string = nulltype T = Array<string>

var a: T = [“hello” “world”]

Page 14: Getting started with Flow

Even safer code

function size(str: ?string): number {

return str.length;}

size(2);

if (!str) {retur

n 0;}

// Error: Property number…

Page 15: Getting started with Flow

Getting started1. Install Flow

2. Prefix your JS file with the /* @flow weak */ annotation

3. Run Flow at the root of your project

Page 16: Getting started with Flow

@flow weak

• Infers type of unannotated variables as `any`

• Provides a manageable amount of errors to fix

• Ignores unknown modules

Page 17: Getting started with Flow

Declarations

declare var __dirname: string;

declare function setTimeout(callback: () => void , ms: number): any;

Stub unknown/browser/node methods

Page 18: Getting started with Flow

Build tools

• gulp-flowtype

• sbt-flow

• grunt-flow

Page 20: Getting started with Flow

@charliedowler

Questions?


Recommended