jazoon'13 - oliver zeigermann - typed javascript with typescript

22
Typed JavaScript with TypeScript Oliver Zeigermann

Upload: jazoon13

Post on 08-May-2015

738 views

Category:

Technology


0 download

DESCRIPTION

http://guide13.jazoon.com/#/submissions/61

TRANSCRIPT

Page 1: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

Typed JavaScript with TypeScriptOliver Zeigermann

Page 2: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

| zeigermann.eu

What is TypeScript?• Programming

Language

• Compiles to JavaScript

• Every valid JavaScript

program also is a valid

TypeScript program

• Superset of JavaScript

• Adds Declared Types

• Reduces boiler plate code

• Developed by Microsoft

• Head: Anders Hejlsberg

Page 3: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

| zeigermann.eu

TypeScript

http://www.typescriptlang.org

Page 4: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

| zeigermann.eu

Types

Page 5: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

• TypeScript offers optional static typing

• Type Inference can infer types even when

you do not declare them

• Declared Types enable reliable IDE

support

| zeigermann.eu

Declared static types

Page 6: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

| zeigermann.eu

Reliable IDE support for• Code Analysis

• Code Completion

• Quick Fixes

• Refactoring

• Type Hierarchies

• Outline

Page 7: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

not reliable!

•Ever had a non-reliable Internet Connection?

•Remember how that felt?

•You want refactoring and code analysis to be

reliable | zeigermann.eu

JavaScript IDEs offer that

without declared types, but…

Page 8: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

var name = "Olli";

function doIt(p1, p2, p3) {

}

doIt(name);

| zeigermann.eu

Optional Declared Types:

Basics

Page 9: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

var name: string = "Olli";

function doIt(p1: number, p2: boolean, p3: string): void

{

}

doIt(name); | zeigermann.eu

Optional Declared Types:

Basics

Page 10: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

• Best IDEs

• WebStorm / IntelliJ IDEA Ultimate

• Visual Studio (Express only with limited support)

• Full refactoring / Code completion etc.

• Not quite at the level of Java-IDEs, yet

| zeigermann.eu

IDE Support

Page 11: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

| zeigermann.eu

Syntactic Sugar

Page 12: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

| zeigermann.eu

JavaScript can express all

this• classes

• interfaces

• inheritance

• modules

• optional and default

parameters

• and more…

Page 13: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

__extends(Horse, Animal);

function Horse(name) {

Animal.call(this, name);

}

Horse.prototype.move = function () {

alert("Galloping...");

Animal.prototype.move.call(this, 45);

};

| zeigermann.eu

JavaScript for Inheritance

Page 14: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

• TypeScript offers syntactic sugar for those

patterns

• No other modifications to language

• Compiler spits out best practice code

| zeigermann.eu

Boiler Plate Code sucks

Page 15: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

class Horse extends Animal {

constructor(name: string) { super(name); }

move(): void {

alert("Galloping...");

super.move(45);

}

}

| zeigermann.eu

TypeScript for Inheritance

Page 16: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

| zeigermann.eu

Compare

Page 17: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

| zeigermann.eu

Comparing to CoffeeScriptCoffeeScript has in

common

•Compiles to JavaScript

•Classes and inheritance

as syntactic sugar

CoffeeScript differs

•Semantics (a little)

different from JavaScript

•No static type information

•Fixes lexical scoping

Page 18: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

| zeigermann.eu

Comparing to DartDart has in common

•Optional static typing

including Generics

•Runs on Client and

Server

•Compiles to JavaScript

Dart differs

•Semantics different from

JavaScript

•Can also be executed

natively in dedicated VM

Page 19: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

• Optional Declared Types enable premium IDE

support

• Syntactic sugar reduces boiler plate code

• Still totally compatible with JavaScript

• Added features aligned with ECMAScript Harmony

• Dart and CoffeeScript both differ in Philosophy | zeigermann.eu

Wrap-Up

Page 20: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

• Business Logic: totally

• UI Logic only partially benefits from types

when accessed from template

• Mixture of typed and untyped not an issue

• Backend and frontend both cool

| zeigermann.eu

Where to apply?

Page 21: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

| zeigermann.eu

And there is more!• Mapping files

• External module

declarations for AMD /

CommonJS

• Enums

• Generics

• Casts

• All the ES 6 goodness

• Declaration files for

JavaScript libraries

Page 22: JAZOON'13 - Oliver Zeigermann - Typed JavaScript with TypeScript

Thanks for the attention!Questions / Discussion

Follow @[email protected]

zeigermann.eu