4/19/2016Footer Text 1Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
One language to rule them all:
TypeScript
Gil Fink
CEO and Senior Consultant, sparXys
4/19/2016Footer Text 2Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
About Me
• sparXys CEO and senior consultant
• ASP.NET/IIS Microsoft MVP in the last 7 years
• Pro Single Page Application Development (Apress)
co-author
• 4 Microsoft Official Courses (MOCs) co-author
• GDG Rashlatz and ng-conf Israel co-organizer
4/19/2016Footer Text 3Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Agenda
• The Why
• TypeScript Syntax and Language Features
• Building a Simple App with TypeScript
• Summary
4/19/2016Footer Text 4Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Wait!
JavaScript?
Are you nuts?
4/19/2016Footer Text 5Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
4/19/2016Footer Text 6Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
"JavaScript is the assembly
language of the Web"
Erik Meijer
4/19/2016Footer Text 7Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
“You can write large
programs in JavaScript. You
just can’t maintain them”
Anders Hejlsberg
4/19/2016Footer Text 8Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
JavaScript isn’t Really Bad
• JavaScript is really a powerful language:
o Functional
o Dynamic
o Can run everywhere
• Huge community
• Libraries
• Tools
o IDEs
o Debuggers
o Test tools
4/19/2016Footer Text 9Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Some Alternatives
• We have several alternatives:
• Hard core JavaScript development – my Stockholm syndrome
• JavaScript transpilers/preprocessors
• CoffeeScript – http://coffeescript.org
• Dart – http://dartlang.org
• Clojurescript - https://github.com/clojure/clojurescript
• Script# - http://scriptsharp.com/
4/19/2016Footer Text 10Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
What is TypeScript?
“TypeScript is a typed superset of JavaScript that
compiles to plain JavaScript” ~typescriptlang.org
4/19/2016Footer Text 11Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Hello TypeScript
Demo
4/19/2016Footer Text 12Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
TypeScript is Very
Flexible
Any Browser Any Host
Any OS Tool Support
4/19/2016Footer Text 13Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Some TypeScript Key
Features
Support
standard
JavaScript
code with
static typing
Encapsulation
through classes
and modules
Support for
constructors,
properties and
functions
Interfaces and
enums
support
Lambda and
generics
support
Intellisense
and syntax
checking
4/19/2016Footer Text 14Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
• Modules
• Classes
• Arrow functions
• Default parameters
• Destructuring
• Spread and rest
• Let and const
• for...of
• Object literal
methods
• Shorthand
properties
• Computed
properties
• Octal / binary
literals
• Symbols
• Template strings
Features from the near Future of
the Web (ES2015/6), Today
Choose your
compilation scenario.
It is up to you!
4/19/2016Footer Text 15Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
From TypeScript to
JavaScript
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return “Hi," + this.greeting;
}
}
TypeScript Code JavaScript Code
TypeScript
Compiler
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet =
function () {
return “Hi," + this.greeting;
};
return Greeter;
})();
tsc.js
4/19/2016Footer Text 16Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
tsconfig.json
• Enables to configure the compiler options:
o Target language (ES3, ES5, ES6)
o Module system (AMD, ES6, CommonJS and etc.)
o Source map generation
o Remove comments when compiling
o And more
4/19/2016Footer Text 17Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
tsconfig.json
Demo
17
4/19/2016Footer Text 18Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
How Good is TypeScript’s
Output?
4/19/2016Footer Text 19Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Some Important Side
Notes
• All JavaScript code is TypeScript code
o Simply copy and paste
• All JavaScript libraries work with TypeScript
o You will need a declaration file to work with the library
4/19/2016Footer Text 20Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Definitely Typed + Typings
https://github.com/DefinitelyTyped/DefinitelyTyped
https://github.com/typings/typings
Demo
4/19/2016Footer Text 21Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
TypeScript Type
Annotations
• You can add type annotations to variables and
functions
var str: string = ‘hello’; // str is annotated as string
function foo(name: string) : string { // parameter and function annotated
return ‘hello’ + name;
}
4/19/2016Footer Text 22Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
TypeScript Types
4/19/2016Footer Text 23Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Type Annotations
Demo
4/19/2016Footer Text 24Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Classes and Interfaces
• You can define classes
• You can define interfaces
o And implement them later
interface IGreeter {
greet(): void;
}
class Greeter implements IGreeter{
greeting: string;
greet() {
console.log(this.greeting);
}
}
var Greeter = (function () {
function Greeter() {
}
Greeter.prototype.greet = function () {
console.log(this.greeting);
};
return Greeter;
})();
4/19/2016Footer Text 25Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Modules
• You define modules to wrap classes, interfaces and
functions
• Use import and export keywords
•module app {
export interface IGreeter {
greet(): void;
}
export class Greeter implements IGreeter {
greeting: string;
greet() {
console.log(this.greeting);
}
}
}
var app;
(function (app) {
var Greeter = (function () {
function Greeter() {
}
Greeter.prototype.greet = function () {
console.log(this.greeting);
};
return Greeter;
})();
app.Greeter = Greeter;
})(app || (app = {}));
4/19/2016Footer Text 26Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Modules – cont.
• It is preferable to use ES2015 modules syntax
o No need to use the module keyword
26
export interface IGreeter {
greet(): void;
}
export class Greeter implements
IGreeter {
greeting: string;
greet() {
console.log(this.greeting);
}
}
var Greeter = (function () {
function Greeter() {
}
Greeter.prototype.greet = function ()
{
console.log(this.greeting);
};
return Greeter;
}());
exports.Greeter = Greeter;
4/19/2016Footer Text 27Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Classes, Modules and Interfaces
Demo
4/19/2016Footer Text 28Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Building a Simple End-to-End App with
TypeScript
Demo
4/19/2016Footer Text 29Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
TypeScript Versions
• TypeScript 1.0 – 1.6
• TypeScript 1.7
o Async/Await for ES6 targets
o Polymorphic this Typing
o ES6 Module Emitting
o ES7 Exponentiation
• Current version: TypeScript 1.8
• TypeScript 2.0 (vNext no release in the near future)
4/19/2016Footer Text 30Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Questions?
4/19/2016Footer Text 31Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Summary
• Open source language that compiles into
JavaScript
• Key features:
• Code encapsulation
• Maintainable code
• Tooling support
• Learn TypeScript today!
4/19/2016Footer Text 32Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Resources
• TypeScript – http://www.typescriptlang.org
• TypeScript Source Code -
https://github.com/Microsoft/TypeScript
• Definitely Typed –
https://github.com/borisyankov/DefinitelyTyped
• My Website – http://www.gilfink.net
• Follow me on Twitter – @gilfink
4/19/2016Footer Text 33Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Thank You!

One language to rule them all type script

  • 1.
    4/19/2016Footer Text 1Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek One language to rule them all: TypeScript Gil Fink CEO and Senior Consultant, sparXys
  • 2.
    4/19/2016Footer Text 2Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek About Me • sparXys CEO and senior consultant • ASP.NET/IIS Microsoft MVP in the last 7 years • Pro Single Page Application Development (Apress) co-author • 4 Microsoft Official Courses (MOCs) co-author • GDG Rashlatz and ng-conf Israel co-organizer
  • 3.
    4/19/2016Footer Text 3Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Agenda • The Why • TypeScript Syntax and Language Features • Building a Simple App with TypeScript • Summary
  • 4.
    4/19/2016Footer Text 4Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Wait! JavaScript? Are you nuts?
  • 5.
    4/19/2016Footer Text 5Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
  • 6.
    4/19/2016Footer Text 6Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek "JavaScript is the assembly language of the Web" Erik Meijer
  • 7.
    4/19/2016Footer Text 7Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek “You can write large programs in JavaScript. You just can’t maintain them” Anders Hejlsberg
  • 8.
    4/19/2016Footer Text 8Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek JavaScript isn’t Really Bad • JavaScript is really a powerful language: o Functional o Dynamic o Can run everywhere • Huge community • Libraries • Tools o IDEs o Debuggers o Test tools
  • 9.
    4/19/2016Footer Text 9Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Some Alternatives • We have several alternatives: • Hard core JavaScript development – my Stockholm syndrome • JavaScript transpilers/preprocessors • CoffeeScript – http://coffeescript.org • Dart – http://dartlang.org • Clojurescript - https://github.com/clojure/clojurescript • Script# - http://scriptsharp.com/
  • 10.
    4/19/2016Footer Text 10Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek What is TypeScript? “TypeScript is a typed superset of JavaScript that compiles to plain JavaScript” ~typescriptlang.org
  • 11.
    4/19/2016Footer Text 11Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Hello TypeScript Demo
  • 12.
    4/19/2016Footer Text 12Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek TypeScript is Very Flexible Any Browser Any Host Any OS Tool Support
  • 13.
    4/19/2016Footer Text 13Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Some TypeScript Key Features Support standard JavaScript code with static typing Encapsulation through classes and modules Support for constructors, properties and functions Interfaces and enums support Lambda and generics support Intellisense and syntax checking
  • 14.
    4/19/2016Footer Text 14Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek • Modules • Classes • Arrow functions • Default parameters • Destructuring • Spread and rest • Let and const • for...of • Object literal methods • Shorthand properties • Computed properties • Octal / binary literals • Symbols • Template strings Features from the near Future of the Web (ES2015/6), Today Choose your compilation scenario. It is up to you!
  • 15.
    4/19/2016Footer Text 15Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek From TypeScript to JavaScript class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } greet() { return “Hi," + this.greeting; } } TypeScript Code JavaScript Code TypeScript Compiler var Greeter = (function () { function Greeter(message) { this.greeting = message; } Greeter.prototype.greet = function () { return “Hi," + this.greeting; }; return Greeter; })(); tsc.js
  • 16.
    4/19/2016Footer Text 16Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek tsconfig.json • Enables to configure the compiler options: o Target language (ES3, ES5, ES6) o Module system (AMD, ES6, CommonJS and etc.) o Source map generation o Remove comments when compiling o And more
  • 17.
    4/19/2016Footer Text 17Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek tsconfig.json Demo 17
  • 18.
    4/19/2016Footer Text 18Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek How Good is TypeScript’s Output?
  • 19.
    4/19/2016Footer Text 19Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Some Important Side Notes • All JavaScript code is TypeScript code o Simply copy and paste • All JavaScript libraries work with TypeScript o You will need a declaration file to work with the library
  • 20.
    4/19/2016Footer Text 20Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Definitely Typed + Typings https://github.com/DefinitelyTyped/DefinitelyTyped https://github.com/typings/typings Demo
  • 21.
    4/19/2016Footer Text 21Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek TypeScript Type Annotations • You can add type annotations to variables and functions var str: string = ‘hello’; // str is annotated as string function foo(name: string) : string { // parameter and function annotated return ‘hello’ + name; }
  • 22.
    4/19/2016Footer Text 22Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek TypeScript Types
  • 23.
    4/19/2016Footer Text 23Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Type Annotations Demo
  • 24.
    4/19/2016Footer Text 24Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Classes and Interfaces • You can define classes • You can define interfaces o And implement them later interface IGreeter { greet(): void; } class Greeter implements IGreeter{ greeting: string; greet() { console.log(this.greeting); } } var Greeter = (function () { function Greeter() { } Greeter.prototype.greet = function () { console.log(this.greeting); }; return Greeter; })();
  • 25.
    4/19/2016Footer Text 25Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Modules • You define modules to wrap classes, interfaces and functions • Use import and export keywords •module app { export interface IGreeter { greet(): void; } export class Greeter implements IGreeter { greeting: string; greet() { console.log(this.greeting); } } } var app; (function (app) { var Greeter = (function () { function Greeter() { } Greeter.prototype.greet = function () { console.log(this.greeting); }; return Greeter; })(); app.Greeter = Greeter; })(app || (app = {}));
  • 26.
    4/19/2016Footer Text 26Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Modules – cont. • It is preferable to use ES2015 modules syntax o No need to use the module keyword 26 export interface IGreeter { greet(): void; } export class Greeter implements IGreeter { greeting: string; greet() { console.log(this.greeting); } } var Greeter = (function () { function Greeter() { } Greeter.prototype.greet = function () { console.log(this.greeting); }; return Greeter; }()); exports.Greeter = Greeter;
  • 27.
    4/19/2016Footer Text 27Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Classes, Modules and Interfaces Demo
  • 28.
    4/19/2016Footer Text 28Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Building a Simple End-to-End App with TypeScript Demo
  • 29.
    4/19/2016Footer Text 29Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek TypeScript Versions • TypeScript 1.0 – 1.6 • TypeScript 1.7 o Async/Await for ES6 targets o Polymorphic this Typing o ES6 Module Emitting o ES7 Exponentiation • Current version: TypeScript 1.8 • TypeScript 2.0 (vNext no release in the near future)
  • 30.
    4/19/2016Footer Text 30Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Questions?
  • 31.
    4/19/2016Footer Text 31Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Summary • Open source language that compiles into JavaScript • Key features: • Code encapsulation • Maintainable code • Tooling support • Learn TypeScript today!
  • 32.
    4/19/2016Footer Text 32Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Resources • TypeScript – http://www.typescriptlang.org • TypeScript Source Code - https://github.com/Microsoft/TypeScript • Definitely Typed – https://github.com/borisyankov/DefinitelyTyped • My Website – http://www.gilfink.net • Follow me on Twitter – @gilfink
  • 33.
    4/19/2016Footer Text 33Jointhe conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Thank You!