typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft,...

86
TypeScript future and past othree @ modern web conf

Transcript of typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft,...

Page 1: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

TypeScriptfuture and past

othree @ modern web conf

Page 2: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Notice

Codes in this slide might be invalid

Page 3: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Notice

Codes in this slide might be invalid Even in the future

Page 4: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Type

• JavaScript is dynamic type

• No type check at compile time and run time

Page 5: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

var hey

hey = 1

hey = 'this is string'

hey = false

Page 6: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

int hey

hey = 1

hey = 'this is string' // Error

hey = false // Error

Page 7: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Pros

• Compiler optimization

• Reliability for large scale app

• IDE support

http://stackoverflow.com/questions/125367/dynamic-type-languages-versus-static-type-languages

Page 8: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8
Page 9: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

ECMAScript 4

• Lots of new features

• Type annotation

• Static type check

http://www.ecmascript.org/es4/spec/overview.pdf

Page 10: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

var hey:number

hey = 1

hey = 'this is string’ // TypeError

hey = false // TypeError

Page 11: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

var ho = { id: 123, desc: "hoho"} : { id: int, desc: string}

Page 12: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

type Tuple = { id: int, desc: string}

var ho = { id: 123, desc: "hoho"} : Tuple

Page 13: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

ECMAScript 4

• Deprecated to be ECMAScript standard

• Live in ActionScript 3

• Flash, Flex

Page 14: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8
Page 15: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

• Type in compile to JavaScript languages

Page 16: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8
Page 17: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

TypeScript

• Microsoft, 2012

• Add type and several features to JavaScript(ES5)

• JavaScript Superset

Page 18: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

TypeScript

Type Class

Generics Module

Page 19: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Type

• Optional type annotation

• Compile time type check

• Type definition file

Page 20: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

var hey:number

hey = 1

hey = 'this is string’ // Error

hey = false // Error

Page 21: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

var hey:number

hey = 1

hey = 'this is string’ // Compile Error

hey = false // Compile Error

Page 22: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

var hey

hey = 1

Page 23: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

interface Tuple { id: number; desc: string;}

var ho:Tuple = { id: 123, desc: "hoho"}

Page 24: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Definition File

• Like C++ header file

• Define library interface

• File extension: .d.ts

• Work with Visual Studio, TernJS

Page 25: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8
Page 26: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

700+ libs

Page 27: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8
Page 28: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Projects

• AngularJS 2

• Asana

• Immutable.js

• Shumway by Mozilla

Page 29: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

TypeScript 1.5+

• Align to ECMAScript 6

• Use native module and class

• More ECMAScript 6 features

http://blogs.msdn.com/b/typescript/archive/2014/10/22/typescript-and-the-road-to-2-0.aspx

Page 30: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

http://goo.gl/pwk6Pb

Page 31: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

http://goo.gl/pwk6Pb

Page 32: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Angular Team not Satisfy

Page 33: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

AtScript

• Google Angular Team, 2014

• Annotation, Introspection

• At means @

Page 34: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

http://atscript.org

Page 35: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

http://goo.gl/pwk6Pb

Page 36: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

http://goo.gl/pwk6Pb

Page 37: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Annotation

• Store meta data

• Accessible in runtime

• Like Java annotation

Page 38: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

@Memorizefunction fib(n) { if (n < 2) { return n } return fib(n - 1) + fib(n - 2)}

Page 39: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

function fib(n) { if (n < 2) { return n } return fib(n - 1) + fib(n - 2)}

fib.annotations = [ new Memorize()]

Page 40: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Runtime Readable

• Use `new` to create a new instance

• Store under `annotations`

Page 41: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Introspection

• Runtime type check

Page 42: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Runtime Type Check

• No magic

• Add code to check type

• Use assert.js

Page 43: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8
Page 44: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

function fib(n:number):number { if (n < 2) { return n } return fib(n - 1) + fib(n - 2)}

Page 45: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

function fib(n) { assert.argumentTypes(n, number) if (n < 2) { return assert.returnType((n), number) } return assert.returnType( (fib(n - 1) + fib(n - 2)), number )}

Page 46: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

function fib(n) { assert.argumentTypes(n, number) if (n < 2) { return assert.returnType((n), number) } return assert.returnType( (fib(n - 1) + fib(n - 2)), number )}

Page 47: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

function fib(n) { assert.argumentTypes(n, number) if (n < 2) { return assert.returnType((n), number) } return assert.returnType( (fib(n - 1) + fib(n - 2)), number )}

Page 48: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

function fib(n) { assert.argumentTypes(n, number); if (n < 2) { return assert.returnType((n), number); } return assert.returnType( (fib(n - 1) + fib(n - 2)), number );}

Page 49: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Performance Impact

• Yes, of course

• Only run type check at development time

• Compile to no type check version for production

Page 50: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

AtScript Compiler

• Use traceur with options

Page 51: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

AtScript Playground

• Traceur environment ready for play

https://github.com/angular/atscript-playground

Page 52: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

https://github.com/angular/atscript-playground/blob/master/config.json

{ "traceur": { "modules": "amd", "script": false, "types": true, "typeAssertions": true, "typeAssertionModule": "assert", "annotations": true, "sourceMaps": "file" }}

Page 53: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

{ "traceur": { "modules": "amd", "script": false, "types": true, "typeAssertions": true, "typeAssertionModule": "assert", "annotations": true, "sourceMaps": "file" }}

https://github.com/angular/atscript-playground/blob/master/config.json

Page 54: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Facebook want Their Own Solution

Page 55: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Flow

• Facebook’s static type checker

• Compatible with TypeScript’s syntax

• Several difference

Page 56: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8
Page 57: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Difference

• Doesn’t compile ES6 to ES5

• Scalability, flow analysis

• More types, ex: maybe, non-nullable

• Integrated with JSX

http://www.2ality.com/2014/10/typed-javascript.html

Page 58: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

https://www.youtube.com/watch?v=M8x0bc81smU

Page 59: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8
Page 60: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Old Proposals

Types Old proposal (2009)

Guards Convenient syntax for Trademarks

Trademarks Newer proposal (2011) by Waldemar Horwat

Page 61: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Old Proposals

Types http://wiki.ecmascript.org/doku.php?id=strawman:types

Guards http://wiki.ecmascript.org/doku.php?id=strawman:guards

Trademarks http://wiki.ecmascript.org/doku.php?id=strawman:trademarks

Page 62: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Type

var ho = { id: 123, desc: "hoho"} : { id: int, desc: string}

Page 63: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Guard

var ho = { id :: Integer : 123, desc :: String : "hoho"}

Page 64: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

https://www.youtube.com/watch?v=lGdnh8QSPPk

Page 65: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

http://goo.gl/pwk6Pb

Page 66: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Now

• AtScript no more activity

• Angular 2.0 uses TypeScript

• TypeScript might merge to ES.next

Page 67: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

• Microsoft, Google, Facebook are talking together about type in ECMAScript

Page 68: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

SoundScript

• V8 experiment

• TypeScript compatible syntax

• —strong-mode

https://developers.google.com/v8/experiments#sound

Page 69: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

"use stricter+types";

Page 70: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

https://drive.google.com/file/d/0B1v38H64XQBNT1p2XzFGWWhCR1k/view

Page 71: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

One more thing

Page 72: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Annotation

• Metadata will be parse and use by compiler and runtime

• Type annotation tells the variable data type to compiler

Page 73: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

• How about we want declare some characteristic on objects, methods?

• memorize

• readonly

• ….

Page 74: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Decorator

• Syntax sugar

• Looks like annotation

• Like Python decorator

• by Yehuda Katz

Page 75: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

https://github.com/rwaldron/tc39-notes/blob/master/es6/2014-04/apr-10.md#decorators-for-es7

Page 76: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

https://github.com/Microsoft/TypeScript/issues/1557#issuecomment-77709527

Page 77: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

https://github.com/wycats/javascript-decorators

Page 78: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

class M { @memorize fib(n) { if (n < 2) { return n } return this.fib(n - 1) + this.fib(n - 2) }}

Page 79: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

class M { @memorize fib(n) { if (n < 2) { return n } return this.fib(n - 1) + this.fib(n - 2) }}

Page 80: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

var M = (function () { class M { fib(n) { if (n < 2) { return n } return this.fib(n - 1) + this.fib(n - 2) } }

var _temp _temp = memorize(Foo.prototype, "fib") || _temp if (_temp) Object.defineProperty(M.prototype, "fib", _temp) return M})()

Page 81: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

function memorize(target, name, descriptor) { let getter = descriptor.get, setter = descriptor.set;

descriptor.get = function() { let table = memorizationFor(this); if (name in table) { return table[name]; } return table[name] = getter.call(this); }

descriptor.set = function(val) { let table = memorizationFor(this); setter.call(this, val); table[name] = val; }

return descriptor;}

https://github.com/wycats/javascript-decorators

Page 82: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

http://goo.gl/pwk6Pb

Page 83: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8
Page 84: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

https://github.com/jonathandturner/brainstorming

Page 85: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

• Another version by Jonathan Turner

• Work for TypeScript at Microsoft

• TC39 member, work for decorator now

Page 86: typescript...• Angular 2.0 uses TypeScript • TypeScript might merge to ES.next • Microsoft, Google, Facebook are talking together about type in ECMAScript SoundScript • V8

Questions?