SlideShare a Scribd company logo
WhyStaticTypeCheckingisBetter
@AndrewRota
JavaScript Engineer,
With JavaScript...anything is possible
@AndrewRota | #empirejs 2/48
Change all the types...
@AndrewRota | #empirejs
var x; // x starts undefined
typeof x === 'undefined'
x = 5;  // now it's is a number
typeof x === 'number'
x = 'five'; // now it's a string
typeof x === 'string'
x = true;  // now it's a boolean
typeof x === 'boolean'
x = {value: 'five'}; // now it's an object
typeof x === 'object'
JS
3/48
We have types, but they're not static.
(No type is known or declared at compile time)
@AndrewRota | #empirejs 4/48
7 Types in JavaScript
@AndrewRota | #empirejs
Boolean Number String Object
Symbol Null Undefined
5/48
Most code has type expectations
...we just don't always acknowledge it.
@AndrewRota | #empirejs
function add(arr) {
    var sum = 0;
    for (var i = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;
}
add([2,3]); // returns 5 as expected
add(['foo', 'bar']);  // returns "0foobar" (expected?)
add(null);  // Throws exception: cannot read prop 'length' of null
JS
6/48
What to do?
@AndrewRota | #empirejs 7/48
Ad Hoc Runtime Checks
@AndrewRota | #empirejs
function add(arr) {
    if (!Array.isArray(arr)) return;
    var sum = 0;
    for (var i = 0; i < arr.length; i++) {
        if (typeof arr[i] !== 'number') return;
        sum += arr[i];
    }
    return sum;
}
JS
8/48
Ad Hoc Runtime Checks
@AndrewRota | #empirejs
function add(arr) {
    if (!Array.isArray(arr)) throw('not an array');
    var sum = 0;
    for (var i = 0; i < arr.length; i++) {
        if (typeof arr[i] !== 'number') throw('not a number');
        sum += arr[i];
    }
    return sum;
}
JS
9/48
Type Annotations
@AndrewRota | #empirejs
/**
 * Add all numbers in an array
 *
 * @param {number[]}
 * @return {number}
 */
function add(arr) {
    var sum = 0;
    for (var i = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;
}
JS
10/48
Type Annotations
Explicit typing...without any checks.
@AndrewRota | #empirejs
/**
 * Add all numbers in an array
 *
 * @param {number[]}
 * @return {number}
 */
function add(arr) {
    var sum = 0;
    for (var i = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;
}
JS
11/48
Code Reviews !== Type Checks
@AndrewRota | #empirejs 12/48
Type Checking in JavaScript
@AndrewRota | #empirejs 13/48
ActionScript
Early 2000s | Partially conformed to ECMAScript 4
@AndrewRota | #empirejs
private function add(arr:Array):int{
    var sum:int = 0;
    for (var i:int = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;
}
AS
14/48
Closure Compiler
~2009
@AndrewRota | #empirejs
/**
 * Add all numbers in an array
 *
 * @param {number[]}
 * @return {number}
 */
function add(arr) {
    var sum = 0;
    for (var i = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;
}
JS
15/48
TypeScript
2012 | Compiles to JavaScript, optional static typing
@AndrewRota | #empirejs
function add(arr:Array<number>):number{
    var sum:number = 0;
    for (var i:number = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;
}
TS
16/48
Flow
2014 | Compiles to JavaScript, optional static typing
@AndrewRota | #empirejs
/* @flow */
function add(arr:Array<number>):number{
    var sum:number = 0;
    for (var i:number = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;
}
FLOW
17/48
Flow Comments
2015 | Alternative to a transpile step
@AndrewRota | #empirejs
/* @flow */
function add(arr/*:Array<number>*/)/*:number*/{
    var sum/*:number*/ = 0;
    for (var i/*:number*/ = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;
}
JS
18/48
Released Runtime Env. No Transpile Null Checking ES6
Closure Compiler 2009 Java ✓ X Some
TypeScript 2012 JavaScript X X Some
Flow 2014 OCaml X ✓ Some
Flow Comments 2015 OCaml ✓ ✓ Some
@AndrewRota | #empirejs 19/48
Adding Gradual Type Checks
@AndrewRota | #empirejs 20/48
Step 1: Choose a Type Checker
Step 2: Set Up a Transpile Step
Step 3: Add Type Annotations
@AndrewRota | #empirejs 21/48
Step 1: Choose a Type Checker
Step 2: Set Up a Transpile Step
Step 3: Add Type Annotations
@AndrewRota | #empirejs 22/48
TypeScript Flow
TypeScript vs. Flow
@AndrewRota | #empirejs
Released 2012
Written in JS: any OS
Community-provided
declaration files
Addtional transpiled features
(defaults, overloads)
·
·
·
·
Released 2014
Written in OCaml: OSX, Linux
Built-in null handling.
Comment-only syntax
available
·
·
·
·
23/48
Step 1: Choose a Type Checker
Step 2: Set Up a Transpile Step
Step 3: Add Type Annotations
@AndrewRota | #empirejs 24/48
Setting Up Flow
1. Install Flow from flowtype.org
2. Add transformer ( JSX or Babel ) to your build
@AndrewRota | #empirejs 25/48
Using Flow
1. Run flow check
2. Run build with transformer.
@AndrewRota | #empirejs
Check → Transform
26/48
Using Flow
@AndrewRota | #empirejs 27/48
Setting Up TypeScript
Install TypeScript with npm i ‐g typescript
@AndrewRota | #empirejs 28/48
Using TypeScript
Run tsc myFile.ts
@AndrewRota | #empirejs 29/48
Using TypeScript
@AndrewRota | #empirejs 30/48
Step 1: Choose a Type Checker
Step 2: Set Up a Transpile Step
Step 3: Add Type Annotations
@AndrewRota | #empirejs 31/48
Type Inference
Some of your work's already done!
Flow: property length: Property not found in Number
TypeScript: Property 'length' does not exist on type 'number'
@AndrewRota | #empirejs
var x = 1;
x.length;
JS
32/48
Adding Basic Types
Flow: number, string, boolean, void, Array, Function, Object, mixed, any
TypeScript: number, string, boolean, void, Array, Function, Object, any, enum
@AndrewRota | #empirejs
var x:string = 'test'; TS/FLOW
33/48
Arrays
@AndrewRota | #empirejs
var list:number[] = [1,2,3];
var anotherList:Array<number> = [1,2,3];
TS/FLOW
34/48
Union Types
ThisType | ThatType
@AndrewRota | #empirejs
var x: number | string = 0;
x = 'foo';
TS/FLOW
35/48
Null Checks
Flow has the advantage here
Flow: property x: Property cannot be accessed on possibly null value
TypeScript:  no error
@AndrewRota | #empirejs
var x = null;
x.foo;
JS
36/48
Functions
Both arguments and return values (the function itself)
@AndrewRota | #empirejs
function helloWorld(name: string):string {
   return 'Hello, ' + name;
}
function addExclamation(sentence: string):string {
    return sentence + '!';
}
addExclamation(helloWorld('EmpireJS'));
TS/FLOW
37/48
Object Literals
@AndrewRota | #empirejs
function doSomething(modelObject: {title: string}) {
   return modelObject.title;
}
doSomething({title: 'My Object!', id: 2, flag: true});
TS/FLOW
38/48
Interfaces
External interfaces via declaration files
→ Find TypeScript declarations at definitelytyped.org
@AndrewRota | #empirejs
interface Model {
    title: string
}
function doSomething(modelObject: Model) {
   return modelObject.title;
}
doSomething({title: 'My Object!', id: 2, flag: true});
TS/FLOW
39/48
And there's more!
@AndrewRota | #empirejs
Classes
Modules
Nullable Types
Generics
Polymorphism
·
·
·
·
·
40/48
But is it worth it?
@AndrewRota | #empirejs 41/48
Catch More Bugs at Compile Time
Flow: null: This type is incompatible with string
@AndrewRota | #empirejs
function helloWorld(name:string) {
    return 'Hello, ' + name;
}
helloWorld('EmpireJS');
helloWorld(null);
        
FLOW
42/48
Self-Document Code Behavior
@AndrewRota | #empirejs
function flatten (input, shallow, strict) {}
        
JS
function flatten (
    input: Array<any>,
    shallow: boolean,
    strict: boolean
): Array<any> {}
        
TS/FLOW
source: http://flowtype.org/docs/underscore.html#_
43/48
Easier to Reason About Code Flow
@AndrewRota | #empirejs
In: Number
Out: String
↗
In: String
Out: Object
↗
In: Object
Out: Boolean
44/48
- Bertrand Meyer
Correctness
“ The ability of software products to perform their exact tasks, as
defined by their specification.
@AndrewRota | #empirejs 45/48
Tooling
IntelliJ
Sublime
@AndrewRota | #empirejs 46/48
- TypeScript and the Road to 2.0
And someday it might be in JavaScript...
“ The TypeScript team is [...] looking forward to working together going
forward and creating the best tools we can for the JavaScript community.
In the long term, we will also be working to fold the best features of
these tools into ECMAScript, the standard behind JavaScript.
@AndrewRota | #empirejs 47/48
Thanks!
twitter @andrewrota
github github.com/andrewrota

More Related Content

What's hot

Review of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiReview of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iii
Nico Ludwig
 
Reconciling Functional Programming and Exceptions
Reconciling Functional Programming and ExceptionsReconciling Functional Programming and Exceptions
Reconciling Functional Programming and Exceptions
Seamus Mc Gonigle
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
SadhanaParameswaran
 
JavaScripts internals #1
JavaScripts internals #1JavaScripts internals #1
JavaScripts internals #1
Martin Pernica
 
All You Need to Know About Type Script
All You Need to Know About Type ScriptAll You Need to Know About Type Script
All You Need to Know About Type Script
Folio3 Software
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
C#4.0 features
C#4.0 featuresC#4.0 features
Type Checking JavaScript
Type Checking JavaScriptType Checking JavaScript
Type Checking JavaScript
Pascal-Louis Perez
 
FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)
Dierk König
 
Academy PRO: React JS. Typescript
Academy PRO: React JS. TypescriptAcademy PRO: React JS. Typescript
Academy PRO: React JS. Typescript
Binary Studio
 
Start with swift
Start with swiftStart with swift
Swift, a quick overview
Swift, a quick overviewSwift, a quick overview
Swift, a quick overview
Julian Król
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3Chris Farrell
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming PatternsVasil Remeniuk
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
Togakangaroo
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
georgebrock
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
Jayaprakash R
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course yoavrubin
 
JavaScript: Core Part
JavaScript: Core PartJavaScript: Core Part
JavaScript: Core Part
維佋 唐
 

What's hot (20)

Review of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiReview of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iii
 
Reconciling Functional Programming and Exceptions
Reconciling Functional Programming and ExceptionsReconciling Functional Programming and Exceptions
Reconciling Functional Programming and Exceptions
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
JavaScripts internals #1
JavaScripts internals #1JavaScripts internals #1
JavaScripts internals #1
 
All You Need to Know About Type Script
All You Need to Know About Type ScriptAll You Need to Know About Type Script
All You Need to Know About Type Script
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
C#4.0 features
C#4.0 featuresC#4.0 features
C#4.0 features
 
Type Checking JavaScript
Type Checking JavaScriptType Checking JavaScript
Type Checking JavaScript
 
FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)
 
Academy PRO: React JS. Typescript
Academy PRO: React JS. TypescriptAcademy PRO: React JS. Typescript
Academy PRO: React JS. Typescript
 
Start with swift
Start with swiftStart with swift
Start with swift
 
args_types
args_typesargs_types
args_types
 
Swift, a quick overview
Swift, a quick overviewSwift, a quick overview
Swift, a quick overview
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course
 
JavaScript: Core Part
JavaScript: Core PartJavaScript: Core Part
JavaScript: Core Part
 

Viewers also liked

RENNIE COWAN - COMMERCIAL REEL
RENNIE COWAN - COMMERCIAL REEL RENNIE COWAN - COMMERCIAL REEL
RENNIE COWAN - COMMERCIAL REEL
Rennie_Cowan_Films_Art
 
RENNIE COWAN - FILMS
RENNIE COWAN - FILMSRENNIE COWAN - FILMS
RENNIE COWAN - FILMS
Rennie_Cowan_Films_Art
 
Tugas manajemen pemasaran
Tugas manajemen pemasaranTugas manajemen pemasaran
Tugas manajemen pemasaran130293iin
 
Μάγια Ζαχαρίας
Μάγια ΖαχαρίαςΜάγια Ζαχαρίας
Μάγια Ζαχαρίας
nicolaidoumarina
 
Καβάφης Κωνσταντίνος
Καβάφης ΚωνσταντίνοςΚαβάφης Κωνσταντίνος
Καβάφης Κωνσταντίνος
nicolaidoumarina
 
RENNIE COWAN PHOTOGRAPHY
RENNIE COWAN PHOTOGRAPHYRENNIE COWAN PHOTOGRAPHY
RENNIE COWAN PHOTOGRAPHY
Rennie_Cowan_Films_Art
 
RENNIE COWAN - 3D ANIMATION
RENNIE COWAN - 3D ANIMATION RENNIE COWAN - 3D ANIMATION
RENNIE COWAN - 3D ANIMATION
Rennie_Cowan_Films_Art
 
RENNIE COWAN - RED CARPET INTERVIEWS
RENNIE COWAN - RED CARPET INTERVIEWS RENNIE COWAN - RED CARPET INTERVIEWS
RENNIE COWAN - RED CARPET INTERVIEWS
Rennie_Cowan_Films_Art
 
ΟΔΟΣ ΑΡΧΙΕΠΙΣΚΟΠΟΥ ΚΥΠΡΟΥ ΛΕΟΝΤΙΟΥ Α΄
ΟΔΟΣ ΑΡΧΙΕΠΙΣΚΟΠΟΥ ΚΥΠΡΟΥ ΛΕΟΝΤΙΟΥ Α΄ΟΔΟΣ ΑΡΧΙΕΠΙΣΚΟΠΟΥ ΚΥΠΡΟΥ ΛΕΟΝΤΙΟΥ Α΄
ΟΔΟΣ ΑΡΧΙΕΠΙΣΚΟΠΟΥ ΚΥΠΡΟΥ ΛΕΟΝΤΙΟΥ Α΄
nicolaidoumarina
 
Wakayama 1 day
Wakayama 1 dayWakayama 1 day
Wakayama 1 day
Takashi Yasui
 
An Exploration of Frameworks – and Why We Built Our Own
An Exploration of Frameworks – and Why We Built Our OwnAn Exploration of Frameworks – and Why We Built Our Own
An Exploration of Frameworks – and Why We Built Our Own
Andrew Rota
 
RENNIE COWAN - DIRECTOR REEL
RENNIE COWAN - DIRECTOR REEL RENNIE COWAN - DIRECTOR REEL
RENNIE COWAN - DIRECTOR REEL
Rennie_Cowan_Films_Art
 
Short film festivals
Short film festivalsShort film festivals
Short film festivals
pelboy123
 
Μαορι Αιμιλια
Μαορι ΑιμιλιαΜαορι Αιμιλια
Μαορι Αιμιλια
nicolaidoumarina
 
οι Mοϊκανοί (ισαβέλλα&χριστιάνα)
οι Mοϊκανοί (ισαβέλλα&χριστιάνα)οι Mοϊκανοί (ισαβέλλα&χριστιάνα)
οι Mοϊκανοί (ισαβέλλα&χριστιάνα)
nicolaidoumarina
 
insects world
insects worldinsects world
insects world
lenguisopas
 
Warehousing Nagpur
Warehousing NagpurWarehousing Nagpur
Warehousing Nagpurplusgrow
 
Κωνσταντίνος Καβάφης
Κωνσταντίνος ΚαβάφηςΚωνσταντίνος Καβάφης
Κωνσταντίνος Καβάφης
nicolaidoumarina
 

Viewers also liked (20)

RENNIE COWAN - COMMERCIAL REEL
RENNIE COWAN - COMMERCIAL REEL RENNIE COWAN - COMMERCIAL REEL
RENNIE COWAN - COMMERCIAL REEL
 
RENNIE COWAN - FILMS
RENNIE COWAN - FILMSRENNIE COWAN - FILMS
RENNIE COWAN - FILMS
 
Tugas manajemen pemasaran
Tugas manajemen pemasaranTugas manajemen pemasaran
Tugas manajemen pemasaran
 
Μάγια Ζαχαρίας
Μάγια ΖαχαρίαςΜάγια Ζαχαρίας
Μάγια Ζαχαρίας
 
Καβάφης Κωνσταντίνος
Καβάφης ΚωνσταντίνοςΚαβάφης Κωνσταντίνος
Καβάφης Κωνσταντίνος
 
RENNIE COWAN PHOTOGRAPHY
RENNIE COWAN PHOTOGRAPHYRENNIE COWAN PHOTOGRAPHY
RENNIE COWAN PHOTOGRAPHY
 
RENNIE COWAN - 3D ANIMATION
RENNIE COWAN - 3D ANIMATION RENNIE COWAN - 3D ANIMATION
RENNIE COWAN - 3D ANIMATION
 
RENNIE COWAN - RED CARPET INTERVIEWS
RENNIE COWAN - RED CARPET INTERVIEWS RENNIE COWAN - RED CARPET INTERVIEWS
RENNIE COWAN - RED CARPET INTERVIEWS
 
ΟΔΟΣ ΑΡΧΙΕΠΙΣΚΟΠΟΥ ΚΥΠΡΟΥ ΛΕΟΝΤΙΟΥ Α΄
ΟΔΟΣ ΑΡΧΙΕΠΙΣΚΟΠΟΥ ΚΥΠΡΟΥ ΛΕΟΝΤΙΟΥ Α΄ΟΔΟΣ ΑΡΧΙΕΠΙΣΚΟΠΟΥ ΚΥΠΡΟΥ ΛΕΟΝΤΙΟΥ Α΄
ΟΔΟΣ ΑΡΧΙΕΠΙΣΚΟΠΟΥ ΚΥΠΡΟΥ ΛΕΟΝΤΙΟΥ Α΄
 
Wakayama 1 day
Wakayama 1 dayWakayama 1 day
Wakayama 1 day
 
Antena array
Antena arrayAntena array
Antena array
 
Passive voive
Passive voivePassive voive
Passive voive
 
An Exploration of Frameworks – and Why We Built Our Own
An Exploration of Frameworks – and Why We Built Our OwnAn Exploration of Frameworks – and Why We Built Our Own
An Exploration of Frameworks – and Why We Built Our Own
 
RENNIE COWAN - DIRECTOR REEL
RENNIE COWAN - DIRECTOR REEL RENNIE COWAN - DIRECTOR REEL
RENNIE COWAN - DIRECTOR REEL
 
Short film festivals
Short film festivalsShort film festivals
Short film festivals
 
Μαορι Αιμιλια
Μαορι ΑιμιλιαΜαορι Αιμιλια
Μαορι Αιμιλια
 
οι Mοϊκανοί (ισαβέλλα&χριστιάνα)
οι Mοϊκανοί (ισαβέλλα&χριστιάνα)οι Mοϊκανοί (ισαβέλλα&χριστιάνα)
οι Mοϊκανοί (ισαβέλλα&χριστιάνα)
 
insects world
insects worldinsects world
insects world
 
Warehousing Nagpur
Warehousing NagpurWarehousing Nagpur
Warehousing Nagpur
 
Κωνσταντίνος Καβάφης
Κωνσταντίνος ΚαβάφηςΚωνσταντίνος Καβάφης
Κωνσταντίνος Καβάφης
 

Similar to Why Static Type Checking is Better

TypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript applicationTypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript application
Andrea Paciolla
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScript
EPAM Systems
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
KeithMurgic
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston Levi
Winston Levi
 
typescript.pptx
typescript.pptxtypescript.pptx
typescript.pptx
ZeynepOtu
 
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
Nascenia IT
 
JavaScript_introduction_upload.pdf
JavaScript_introduction_upload.pdfJavaScript_introduction_upload.pdf
JavaScript_introduction_upload.pdf
Kongu Engineering College, Perundurai, Erode
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)
Shoaib Ghachi
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
Aleš Najmann
 
Getting started with typescript and angular 2
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2
Knoldus Inc.
 
XAML/C# to HTML/JS
XAML/C# to HTML/JSXAML/C# to HTML/JS
XAML/C# to HTML/JS
Michael Haberman
 
Type script
Type scriptType script
Type script
Zunair Shoes
 
Rits Brown Bag - TypeScript
Rits Brown Bag - TypeScriptRits Brown Bag - TypeScript
Rits Brown Bag - TypeScript
Right IT Services
 
Beauty & the Beast - Java VS TypeScript
Beauty & the Beast - Java VS TypeScriptBeauty & the Beast - Java VS TypeScript
Beauty & the Beast - Java VS TypeScript
Hendrik Ebbers
 
Nodejs & Typescript
Nodejs & TypescriptNodejs & Typescript
Nodejs & Typescript
Knoldus Inc.
 
Mobile Weekend Budapest presentation
Mobile Weekend Budapest presentationMobile Weekend Budapest presentation
Mobile Weekend Budapest presentation
Péter Ádám Wiesner
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
GeorgePeterBanyard
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2ndConnex
 
TensorFlow Lite (r1.5) & Android 8.1 Neural Network API
TensorFlow Lite (r1.5) & Android 8.1 Neural Network APITensorFlow Lite (r1.5) & Android 8.1 Neural Network API
TensorFlow Lite (r1.5) & Android 8.1 Neural Network API
Mr. Vengineer
 

Similar to Why Static Type Checking is Better (20)

TypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript applicationTypeScript introduction to scalable javascript application
TypeScript introduction to scalable javascript application
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScript
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston Levi
 
typescript.pptx
typescript.pptxtypescript.pptx
typescript.pptx
 
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
 
JavaScript_introduction_upload.pdf
JavaScript_introduction_upload.pdfJavaScript_introduction_upload.pdf
JavaScript_introduction_upload.pdf
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
 
Getting started with typescript and angular 2
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2
 
XAML/C# to HTML/JS
XAML/C# to HTML/JSXAML/C# to HTML/JS
XAML/C# to HTML/JS
 
Type script
Type scriptType script
Type script
 
Rits Brown Bag - TypeScript
Rits Brown Bag - TypeScriptRits Brown Bag - TypeScript
Rits Brown Bag - TypeScript
 
Beauty & the Beast - Java VS TypeScript
Beauty & the Beast - Java VS TypeScriptBeauty & the Beast - Java VS TypeScript
Beauty & the Beast - Java VS TypeScript
 
Nodejs & Typescript
Nodejs & TypescriptNodejs & Typescript
Nodejs & Typescript
 
Mobile Weekend Budapest presentation
Mobile Weekend Budapest presentationMobile Weekend Budapest presentation
Mobile Weekend Budapest presentation
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
TensorFlow Lite (r1.5) & Android 8.1 Neural Network API
TensorFlow Lite (r1.5) & Android 8.1 Neural Network APITensorFlow Lite (r1.5) & Android 8.1 Neural Network API
TensorFlow Lite (r1.5) & Android 8.1 Neural Network API
 

More from Andrew Rota

Integrating React.js Into a PHP Application: Dutch PHP 2019
Integrating React.js Into a PHP Application: Dutch PHP 2019Integrating React.js Into a PHP Application: Dutch PHP 2019
Integrating React.js Into a PHP Application: Dutch PHP 2019
Andrew Rota
 
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Performant APIs with GraphQL and PHP (Dutch PHP 2019)Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Andrew Rota
 
Getting Started with GraphQL && PHP
Getting Started with GraphQL && PHPGetting Started with GraphQL && PHP
Getting Started with GraphQL && PHP
Andrew Rota
 
Tutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHPTutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHP
Andrew Rota
 
Building a GraphQL API in PHP
Building a GraphQL API in PHPBuilding a GraphQL API in PHP
Building a GraphQL API in PHP
Andrew Rota
 
Ten practical ways to improve front-end performance
Ten practical ways to improve front-end performanceTen practical ways to improve front-end performance
Ten practical ways to improve front-end performance
Andrew Rota
 
Component Based UI Architectures for the Web
Component Based UI Architectures for the WebComponent Based UI Architectures for the Web
Component Based UI Architectures for the Web
Andrew Rota
 
Client-Side Performance Monitoring (MobileTea, Rome)
Client-Side Performance Monitoring (MobileTea, Rome)Client-Side Performance Monitoring (MobileTea, Rome)
Client-Side Performance Monitoring (MobileTea, Rome)
Andrew Rota
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
Andrew Rota
 
Effectively Monitoring Client-Side Performance
Effectively Monitoring Client-Side PerformanceEffectively Monitoring Client-Side Performance
Effectively Monitoring Client-Side Performance
Andrew Rota
 
UI Rendering at Wayfair
UI Rendering at WayfairUI Rendering at Wayfair
UI Rendering at Wayfair
Andrew Rota
 
Better PHP-Frontend Integration with Tungsten.js
Better PHP-Frontend Integration with Tungsten.jsBetter PHP-Frontend Integration with Tungsten.js
Better PHP-Frontend Integration with Tungsten.js
Andrew Rota
 
Tungsten.js: Building a Modular Framework
Tungsten.js: Building a Modular FrameworkTungsten.js: Building a Modular Framework
Tungsten.js: Building a Modular Framework
Andrew Rota
 
The Complementarity of React and Web Components
The Complementarity of React and Web ComponentsThe Complementarity of React and Web Components
The Complementarity of React and Web Components
Andrew Rota
 
Web Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing CombinationWeb Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing Combination
Andrew Rota
 
Bem methodology
Bem methodologyBem methodology
Bem methodology
Andrew Rota
 
Web Components and Modular CSS
Web Components and Modular CSSWeb Components and Modular CSS
Web Components and Modular CSS
Andrew Rota
 

More from Andrew Rota (17)

Integrating React.js Into a PHP Application: Dutch PHP 2019
Integrating React.js Into a PHP Application: Dutch PHP 2019Integrating React.js Into a PHP Application: Dutch PHP 2019
Integrating React.js Into a PHP Application: Dutch PHP 2019
 
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Performant APIs with GraphQL and PHP (Dutch PHP 2019)Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
 
Getting Started with GraphQL && PHP
Getting Started with GraphQL && PHPGetting Started with GraphQL && PHP
Getting Started with GraphQL && PHP
 
Tutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHPTutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHP
 
Building a GraphQL API in PHP
Building a GraphQL API in PHPBuilding a GraphQL API in PHP
Building a GraphQL API in PHP
 
Ten practical ways to improve front-end performance
Ten practical ways to improve front-end performanceTen practical ways to improve front-end performance
Ten practical ways to improve front-end performance
 
Component Based UI Architectures for the Web
Component Based UI Architectures for the WebComponent Based UI Architectures for the Web
Component Based UI Architectures for the Web
 
Client-Side Performance Monitoring (MobileTea, Rome)
Client-Side Performance Monitoring (MobileTea, Rome)Client-Side Performance Monitoring (MobileTea, Rome)
Client-Side Performance Monitoring (MobileTea, Rome)
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
 
Effectively Monitoring Client-Side Performance
Effectively Monitoring Client-Side PerformanceEffectively Monitoring Client-Side Performance
Effectively Monitoring Client-Side Performance
 
UI Rendering at Wayfair
UI Rendering at WayfairUI Rendering at Wayfair
UI Rendering at Wayfair
 
Better PHP-Frontend Integration with Tungsten.js
Better PHP-Frontend Integration with Tungsten.jsBetter PHP-Frontend Integration with Tungsten.js
Better PHP-Frontend Integration with Tungsten.js
 
Tungsten.js: Building a Modular Framework
Tungsten.js: Building a Modular FrameworkTungsten.js: Building a Modular Framework
Tungsten.js: Building a Modular Framework
 
The Complementarity of React and Web Components
The Complementarity of React and Web ComponentsThe Complementarity of React and Web Components
The Complementarity of React and Web Components
 
Web Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing CombinationWeb Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing Combination
 
Bem methodology
Bem methodologyBem methodology
Bem methodology
 
Web Components and Modular CSS
Web Components and Modular CSSWeb Components and Modular CSS
Web Components and Modular CSS
 

Recently uploaded

FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
ViralQR
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 

Why Static Type Checking is Better

  • 2. With JavaScript...anything is possible @AndrewRota | #empirejs 2/48
  • 3. Change all the types... @AndrewRota | #empirejs var x; // x starts undefined typeof x === 'undefined' x = 5;  // now it's is a number typeof x === 'number' x = 'five'; // now it's a string typeof x === 'string' x = true;  // now it's a boolean typeof x === 'boolean' x = {value: 'five'}; // now it's an object typeof x === 'object' JS 3/48
  • 4. We have types, but they're not static. (No type is known or declared at compile time) @AndrewRota | #empirejs 4/48
  • 5. 7 Types in JavaScript @AndrewRota | #empirejs Boolean Number String Object Symbol Null Undefined 5/48
  • 6. Most code has type expectations ...we just don't always acknowledge it. @AndrewRota | #empirejs function add(arr) {     var sum = 0;     for (var i = 0; i < arr.length; i++) {         sum += arr[i];     }     return sum; } add([2,3]); // returns 5 as expected add(['foo', 'bar']);  // returns "0foobar" (expected?) add(null);  // Throws exception: cannot read prop 'length' of null JS 6/48
  • 7. What to do? @AndrewRota | #empirejs 7/48
  • 8. Ad Hoc Runtime Checks @AndrewRota | #empirejs function add(arr) {     if (!Array.isArray(arr)) return;     var sum = 0;     for (var i = 0; i < arr.length; i++) {         if (typeof arr[i] !== 'number') return;         sum += arr[i];     }     return sum; } JS 8/48
  • 9. Ad Hoc Runtime Checks @AndrewRota | #empirejs function add(arr) {     if (!Array.isArray(arr)) throw('not an array');     var sum = 0;     for (var i = 0; i < arr.length; i++) {         if (typeof arr[i] !== 'number') throw('not a number');         sum += arr[i];     }     return sum; } JS 9/48
  • 10. Type Annotations @AndrewRota | #empirejs /**  * Add all numbers in an array  *  * @param {number[]}  * @return {number}  */ function add(arr) {     var sum = 0;     for (var i = 0; i < arr.length; i++) {         sum += arr[i];     }     return sum; } JS 10/48
  • 11. Type Annotations Explicit typing...without any checks. @AndrewRota | #empirejs /**  * Add all numbers in an array  *  * @param {number[]}  * @return {number}  */ function add(arr) {     var sum = 0;     for (var i = 0; i < arr.length; i++) {         sum += arr[i];     }     return sum; } JS 11/48
  • 12. Code Reviews !== Type Checks @AndrewRota | #empirejs 12/48
  • 13. Type Checking in JavaScript @AndrewRota | #empirejs 13/48
  • 14. ActionScript Early 2000s | Partially conformed to ECMAScript 4 @AndrewRota | #empirejs private function add(arr:Array):int{     var sum:int = 0;     for (var i:int = 0; i < arr.length; i++) {         sum += arr[i];     }     return sum; } AS 14/48
  • 15. Closure Compiler ~2009 @AndrewRota | #empirejs /**  * Add all numbers in an array  *  * @param {number[]}  * @return {number}  */ function add(arr) {     var sum = 0;     for (var i = 0; i < arr.length; i++) {         sum += arr[i];     }     return sum; } JS 15/48
  • 16. TypeScript 2012 | Compiles to JavaScript, optional static typing @AndrewRota | #empirejs function add(arr:Array<number>):number{     var sum:number = 0;     for (var i:number = 0; i < arr.length; i++) {         sum += arr[i];     }     return sum; } TS 16/48
  • 17. Flow 2014 | Compiles to JavaScript, optional static typing @AndrewRota | #empirejs /* @flow */ function add(arr:Array<number>):number{     var sum:number = 0;     for (var i:number = 0; i < arr.length; i++) {         sum += arr[i];     }     return sum; } FLOW 17/48
  • 18. Flow Comments 2015 | Alternative to a transpile step @AndrewRota | #empirejs /* @flow */ function add(arr/*:Array<number>*/)/*:number*/{     var sum/*:number*/ = 0;     for (var i/*:number*/ = 0; i < arr.length; i++) {         sum += arr[i];     }     return sum; } JS 18/48
  • 19. Released Runtime Env. No Transpile Null Checking ES6 Closure Compiler 2009 Java ✓ X Some TypeScript 2012 JavaScript X X Some Flow 2014 OCaml X ✓ Some Flow Comments 2015 OCaml ✓ ✓ Some @AndrewRota | #empirejs 19/48
  • 20. Adding Gradual Type Checks @AndrewRota | #empirejs 20/48
  • 21. Step 1: Choose a Type Checker Step 2: Set Up a Transpile Step Step 3: Add Type Annotations @AndrewRota | #empirejs 21/48
  • 22. Step 1: Choose a Type Checker Step 2: Set Up a Transpile Step Step 3: Add Type Annotations @AndrewRota | #empirejs 22/48
  • 23. TypeScript Flow TypeScript vs. Flow @AndrewRota | #empirejs Released 2012 Written in JS: any OS Community-provided declaration files Addtional transpiled features (defaults, overloads) · · · · Released 2014 Written in OCaml: OSX, Linux Built-in null handling. Comment-only syntax available · · · · 23/48
  • 24. Step 1: Choose a Type Checker Step 2: Set Up a Transpile Step Step 3: Add Type Annotations @AndrewRota | #empirejs 24/48
  • 25. Setting Up Flow 1. Install Flow from flowtype.org 2. Add transformer ( JSX or Babel ) to your build @AndrewRota | #empirejs 25/48
  • 26. Using Flow 1. Run flow check 2. Run build with transformer. @AndrewRota | #empirejs Check → Transform 26/48
  • 27. Using Flow @AndrewRota | #empirejs 27/48
  • 28. Setting Up TypeScript Install TypeScript with npm i ‐g typescript @AndrewRota | #empirejs 28/48
  • 31. Step 1: Choose a Type Checker Step 2: Set Up a Transpile Step Step 3: Add Type Annotations @AndrewRota | #empirejs 31/48
  • 32. Type Inference Some of your work's already done! Flow: property length: Property not found in Number TypeScript: Property 'length' does not exist on type 'number' @AndrewRota | #empirejs var x = 1; x.length; JS 32/48
  • 33. Adding Basic Types Flow: number, string, boolean, void, Array, Function, Object, mixed, any TypeScript: number, string, boolean, void, Array, Function, Object, any, enum @AndrewRota | #empirejs var x:string = 'test'; TS/FLOW 33/48
  • 35. Union Types ThisType | ThatType @AndrewRota | #empirejs var x: number | string = 0; x = 'foo'; TS/FLOW 35/48
  • 36. Null Checks Flow has the advantage here Flow: property x: Property cannot be accessed on possibly null value TypeScript:  no error @AndrewRota | #empirejs var x = null; x.foo; JS 36/48
  • 37. Functions Both arguments and return values (the function itself) @AndrewRota | #empirejs function helloWorld(name: string):string {    return 'Hello, ' + name; } function addExclamation(sentence: string):string {     return sentence + '!'; } addExclamation(helloWorld('EmpireJS')); TS/FLOW 37/48
  • 38. Object Literals @AndrewRota | #empirejs function doSomething(modelObject: {title: string}) {    return modelObject.title; } doSomething({title: 'My Object!', id: 2, flag: true}); TS/FLOW 38/48
  • 39. Interfaces External interfaces via declaration files → Find TypeScript declarations at definitelytyped.org @AndrewRota | #empirejs interface Model {     title: string } function doSomething(modelObject: Model) {    return modelObject.title; } doSomething({title: 'My Object!', id: 2, flag: true}); TS/FLOW 39/48
  • 40. And there's more! @AndrewRota | #empirejs Classes Modules Nullable Types Generics Polymorphism · · · · · 40/48
  • 41. But is it worth it? @AndrewRota | #empirejs 41/48
  • 42. Catch More Bugs at Compile Time Flow: null: This type is incompatible with string @AndrewRota | #empirejs function helloWorld(name:string) {     return 'Hello, ' + name; } helloWorld('EmpireJS'); helloWorld(null);          FLOW 42/48
  • 43. Self-Document Code Behavior @AndrewRota | #empirejs function flatten (input, shallow, strict) {}          JS function flatten (     input: Array<any>,     shallow: boolean,     strict: boolean ): Array<any> {}          TS/FLOW source: http://flowtype.org/docs/underscore.html#_ 43/48
  • 44. Easier to Reason About Code Flow @AndrewRota | #empirejs In: Number Out: String ↗ In: String Out: Object ↗ In: Object Out: Boolean 44/48
  • 45. - Bertrand Meyer Correctness “ The ability of software products to perform their exact tasks, as defined by their specification. @AndrewRota | #empirejs 45/48
  • 47. - TypeScript and the Road to 2.0 And someday it might be in JavaScript... “ The TypeScript team is [...] looking forward to working together going forward and creating the best tools we can for the JavaScript community. In the long term, we will also be working to fold the best features of these tools into ECMAScript, the standard behind JavaScript. @AndrewRota | #empirejs 47/48