SlideShare a Scribd company logo
TypeScript 
coding JavaScript 
without the pain 
@Sander_Mak Luminis Technologies
INTRO 
@Sander_Mak: Senior Software Engineer at 
Author: 
Dutch 
Java 
Magazine 
blog @ branchandbound.net 
Speaker:
AGENDA 
Why TypeScript? 
Language introduction / live-coding 
TypeScript and Angular 
Comparison with TS alternatives 
Conclusion
WHAT'S WRONG WITH JAVASCRIPT? 
Dynamic typing 
Lack of modularity 
Verbose patterns (IIFE)
WHAT'S WRONG WITH JAVASCRIPT? 
Dynamic typing 
Lack of modularity 
Verbose patterns (IIFE) 
In short: JavaScript development scales badly
WHAT'S GOOD ABOUT JAVASCRIPT? 
It's everywhere 
Huge amount of libraries 
Flexible
WISHLIST 
Scalable HTML5 clientside development 
Modular development 
Easily learnable for Java developers 
Non-invasive (existing libs, browser support) 
Long-term vision 
Clean JS output (exit strategy)
WISHLIST 
Scalable HTML5 clientside development 
Modular development 
Easily learnable for Java developers 
Non-invasive (existing libs, browser support) 
Long-term vision 
Clean JS output (exit strategy) 
✓✓✓✓✓✓
TypeScript: coding JavaScript without the pain
2.0 licensed
2.0 licensed
TYPESCRIPT 
Superset of JavaScript 
Optionally typed 
Compiles to ES3/ES5 
No special runtime 
1.0 in April 2014, future ES6 alignment
TYPESCRIPT 
Superset of JavaScript 
Optionally typed 
Compiles to ES3/ES5 
No special runtime 
1.0 in April 2014, future ES6 alignment 
In short: Lightweight productivity booster
GETTING STARTED 
$ npm install -g typescript 
$ mv mycode.js mycode.ts 
$ tsc mycode.ts
GETTING STARTED 
$ npm install -g typescript 
$ mv mycode.js mycode.ts 
$ tsc mycode.ts 
May even find problems in existing JS!
OPTIONAL TYPES 
Type annotations 
> var a = 123 
> a.trim() 
! 
TypeError: undefined is 
not a function 
JS 
> var a: string = 123 
> a.trim() 
! 
Cannot convert 'number' 
to 'string'. 
TS 
runtime 
compile-time
OPTIONAL TYPES 
Type annotations 
> var a = 123 
> a.trim() 
! 
TypeError: undefined is 
not a function 
JS 
> var a: string = 123 
> a.trim() 
! 
Cannot convert 'number' 
to 'string'. 
TS 
Type inference 
> var a = 123 
> a.trim() 
! 
The property 'trim' does 
not exist on value of 
type 'number'. 
Types dissapear at runtime
OPTIONAL TYPES 
Object void boolean integer 
long 
short 
... 
String 
char 
Type[] 
any void boolean number string type[]
OPTIONAL TYPES 
Types are structural rather than nominal 
TypeScript has function types: 
var find: (elem: string, elems: string[]) => string = 
function(elem, elems) { 
.. 
}
OPTIONAL TYPES 
Types are structural rather than nominal 
TypeScript has function types: 
var find: (elem: string, elems: string[]) => string = 
function(elem, elems) { 
.. 
}
DEMO: OPTIONAL TYPES 
code 
Code: http://bit.ly/tscode
INTERFACES 
interface MyInterface { 
// Call signature 
(param: number): string 
member: number 
optionalMember?: number 
myMethod(param: string): void 
} 
! 
var instance: MyInterface = ... 
instance(1)
INTERFACES 
Use them to describe data returned in REST calls 
$.getJSON('user/123').then((user: User) => { 
showProfile(user.details) 
}
INTERFACES 
TS interfaces are open-ended: 
interface JQuery { 
appendTo(..): .. 
.. 
} 
interface JQuery { 
draggable(..): .. 
.. 
jquery.d.ts } jquery.ui.d.ts
OPTIONAL TYPES: ENUMS 
enum Language { TypeScript, Java, JavaScript } 
! 
var lang = Language.TypeScript 
var ts = Language[0] 
ts === "TypeScript" 
enum Language { TypeScript = 1, Java, JavaScript } 
! 
var ts = Language[1]
GOING ALL THE WAY 
Force explicit typing with noImplicitAny 
var ambiguousType; 
! 
ambiguousType = 1 
ambiguousType = "text" noimplicitany.ts 
$ tsc --noImplicitAny noimplicitany.ts
GOING ALL THE WAY 
Force explicit typing with noImplicitAny 
var ambiguousType; 
! 
ambiguousType = 1 
ambiguousType = "text" noimplicitany.ts 
$ tsc --noImplicitAny noimplicitany.ts 
error TS7005: Variable 'ambiguousType' implicitly 
has an 'any' type.
TYPESCRIPT CLASSES 
Can implement interfaces 
Inheritance 
Instance methods/members 
Static methods/members 
Single constructor 
Default/optional parameters 
ES6 class syntax 
similar 
different
DEMO: TYPESCRIPT CLASSES 
code 
Code: http://bit.ly/tscode
ARROW FUNCTIONS 
Implicit return 
No braces for single expression 
Part of ES6
ARROW FUNCTIONS 
Implicit return 
No braces for single expression 
Part of ES6 
function(arg1) { 
return arg1.toLowerCase(); 
}
ARROW FUNCTIONS 
Implicit return 
No braces for single expression 
Part of ES6 
function(arg1) { 
return arg1.toLowerCase(); 
} 
(arg1) => arg1.toLowerCase();
ARROW FUNCTIONS 
Implicit return 
No braces for single expression 
Part of ES6 
function(arg1) { 
return arg1.toLowerCase(); 
} 
(arg1) => arg1.toLowerCase(); 
Lexically-scoped this (no more 'var that = this')
DEMO: ARROW FUNCTIONS 
code 
Code: http://bit.ly/tscode
TYPE DEFINITIONS 
How to integrate 
existing JS code? 
Ambient declarations 
Any-type :( 
Type definitions 
lib.d.ts 
Separate compilation: 
tsc --declaration file.ts
TYPE DEFINITIONS 
DefinitelyTyped.org 
Community provided .d.ts 
files for popular JS libs 
How to integrate 
existing JS code? 
Ambient declarations 
Any-type :( 
Type definitions 
lib.d.ts 
Separate compilation: 
tsc --declaration file.ts
INTERNAL MODULES 
module StorageModule { 
export interface Storage { store(content: string): void } 
! 
var privateKey = 'storageKey'; 
export class LocalStorage implements Storage { 
store(content: string): void { 
localStorage.setItem(privateKey, content); 
} 
} 
! 
export class DevNullStorage implements Storage { 
store(content: string): void { } 
} 
} 
! 
var storage: StorageModule.Storage = new StorageModule.LocalStorage(); 
storage.store('testing');
INTERNAL MODULES 
module StorageModule { 
export interface Storage { store(content: string): void } 
! 
var privateKey = 'storageKey'; 
export class LocalStorage implements Storage { 
store(content: string): void { 
localStorage.setItem(privateKey, content); 
} 
} 
! 
export class DevNullStorage implements Storage { 
store(content: string): void { } 
} 
} 
! 
var storage: StorageModule.Storage = new StorageModule.LocalStorage(); 
storage.store('testing');
INTERNAL MODULES 
module StorageModule { 
export interface Storage { store(content: string): void } 
! 
var privateKey = 'storageKey'; 
export class LocalStorage implements Storage { 
store(content: string): void { 
localStorage.setItem(privateKey, content); 
} 
} 
! 
export class DevNullStorage implements Storage { 
store(content: string): void { } 
} 
} 
! 
var storage: StorageModule.Storage = new StorageModule.LocalStorage(); 
storage.store('testing');
INTERNAL MODULES 
module StorageModule { 
export interface Storage { store(content: string): void } 
! 
var privateKey = 'storageKey'; 
export class LocalStorage implements Storage { 
store(content: string): void { 
localStorage.setItem(privateKey, content); 
} 
} 
! 
export class DevNullStorage implements Storage { 
store(content: string): void { } 
} 
} 
! 
var storage: StorageModule.Storage = new StorageModule.LocalStorage(); 
storage.store('testing');
INTERNAL MODULES 
TS internal modules are open-ended: 
! 
module Webshop { 
export class Cart { .. } 
} 
/// <reference path="cart.ts" /> 
module Webshop { 
export class Catalog { .. } 
cart.ts } main.ts
INTERNAL MODULES 
TS internal modules are open-ended: 
! 
module Webshop { 
export class Cart { .. } 
} 
/// <reference path="cart.ts" /> 
module Webshop { 
export class Catalog { .. } 
cart.ts } main.ts 
Can be hierarchical: 
module Webshop.Cart.Backend { 
... 
}
INTERNAL MODULES 
TS internal modules are open-ended: 
! 
module Webshop { 
export class Cart { .. } 
} 
/// <reference path="cart.ts" /> 
module Webshop { 
export class Catalog { .. } 
cart.ts } main.ts 
Can be hierarchical: 
module Webshop.Cart.Backend { 
... 
} 
Combine modules: 
$ tsc --out main.js main.ts
DEMO: PUTTING IT ALL TOGETHER 
code 
Code: http://bit.ly/tscode
EXTERNAL MODULES 
CommonJS 
Asynchronous 
Module 
Definitions 
$ tsc --module common main.ts 
$ tsc --module amd main.ts 
Combine with module loader
EXTERNAL MODULES 
'Standards-based', use existing external modules 
Automatic dependency management 
Lazy loading 
AMD verbose without TypeScript 
Currently not ES6-compatible
DEMO 
+ + 
=
DEMO: TYPESCRIPT AND ANGULAR 
code 
Code: http://bit.ly/tscode
BUILDING TYPESCRIPT 
$ tsc -watch main.ts 
grunt-typescript 
grunt-ts 
gulp-type (incremental) 
gulp-tsc
TOOLING 
IntelliJ IDEA 
WebStorm 
plugin
TYPESCRIPT vs ES6 HARMONY 
Complete language + runtime overhaul 
More features: generators, comprehensions, 
object literals 
Will take years before widely deployed 
No typing (possible ES7)
TYPESCRIPT vs COFFEESCRIPT 
Also a compile-to-JS language 
More syntactic sugar, still dynamically typed 
JS is not valid CoffeeScript 
No spec, definitely no Anders Hejlsberg... 
Future: CS doesn't track ECMAScript 6 
!
TYPESCRIPT vs DART 
Dart VM + stdlib (also compile-to-JS) 
Optionally typed 
Completely different syntax & semantics than JS 
JS interop through dart:js library 
ECMA Dart spec
TYPESCRIPT vs CLOSURE COMPILER 
Google Closure Compiler 
Pure JS 
Types in JsDoc comments 
Less expressive 
Focus on optimization, dead-code removal
WHO USES TYPESCRIPT? 
(duh)
CONCLUSION 
Internal modules 
Classes/Interfaces 
Some typing 
External modules 
Type defs 
More typing 
Generics 
Type defs 
-noImplicitAny
CONCLUSION 
TypeScript allows for gradual adoption 
Internal modules 
Classes/Interfaces 
Some typing 
External modules 
Type defs 
More typing 
Generics 
Type defs 
-noImplicitAny
CONCLUSION 
Some downsides: 
Still need to know some JS quirks 
Current compiler slowish (faster one in the works) 
External module syntax not ES6-compatible (yet) 
Non-MS tooling lagging a bit
CONCLUSION 
High value, low cost improvement over JavaScript 
Safer and more modular 
Solid path to ES6
MORE TALKS 
TypeScript: 
Wednesday, 11:30 AM, same room 
!Akka & Event-sourcing 
Wednesday, 8:30 AM, same room 
@Sander_Mak 
Luminis Technologies
RESOURCES 
Code: http://bit.ly/tscode 
! 
Learn: www.typescriptlang.org/Handbook 
@Sander_Mak 
Luminis Technologies

More Related Content

What's hot (20)

PPTX
Why TypeScript?
FITC
 
PDF
TypeScript Introduction
Dmitry Sheiko
 
PDF
TypeScript - An Introduction
NexThoughts Technologies
 
PPTX
Typescript ppt
akhilsreyas
 
PPT
TypeScript Presentation
Patrick John Pacaña
 
PPTX
Introducing type script
Remo Jansen
 
PPTX
Typescript Fundamentals
Sunny Sharma
 
PDF
TypeScript Best Practices
felixbillon
 
PPTX
Typescript in 30mins
Udaya Kumar
 
PPTX
TypeScript intro
Ats Uiboupin
 
PPTX
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
PPTX
Intro to React
Justin Reock
 
PDF
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
PPTX
Node.js Express
Eyal Vardi
 
PPTX
TypeScript Overview
Aniruddha Chakrabarti
 
PPT
Introduction to Javascript
Amit Tyagi
 
PPT
Spring Core
Pushan Bhattacharya
 
PPTX
Angular 9
Raja Vishnu
 
PDF
ES6 presentation
ritika1
 
PPT
Javascript arrays
Hassan Dar
 
Why TypeScript?
FITC
 
TypeScript Introduction
Dmitry Sheiko
 
TypeScript - An Introduction
NexThoughts Technologies
 
Typescript ppt
akhilsreyas
 
TypeScript Presentation
Patrick John Pacaña
 
Introducing type script
Remo Jansen
 
Typescript Fundamentals
Sunny Sharma
 
TypeScript Best Practices
felixbillon
 
Typescript in 30mins
Udaya Kumar
 
TypeScript intro
Ats Uiboupin
 
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
Intro to React
Justin Reock
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
Node.js Express
Eyal Vardi
 
TypeScript Overview
Aniruddha Chakrabarti
 
Introduction to Javascript
Amit Tyagi
 
Spring Core
Pushan Bhattacharya
 
Angular 9
Raja Vishnu
 
ES6 presentation
ritika1
 
Javascript arrays
Hassan Dar
 

Viewers also liked (17)

PDF
Typescript + Graphql = <3
felixbillon
 
PDF
TypeScript: особенности разработки / Александр Майоров (Tutu.ru)
Ontico
 
PPTX
Typescript tips & tricks
Ori Calvo
 
PDF
Power Leveling your TypeScript
Offirmo
 
PDF
TypeScript Seminar
Haim Michael
 
PPTX
TypeScript
GetDev.NET
 
PDF
Angular 2 - Typescript
Nathan Krasney
 
PDF
TypeScript: Un lenguaje aburrido para programadores torpes y tristes
Micael Gallego
 
PDF
Александр Русаков - TypeScript 2 in action
MoscowJS
 
PDF
TypeScript for Java Developers
Yakov Fain
 
PPTX
Typescript
Nikhil Thomas
 
PPTX
TypeScript
Udaiappa Ramachandran
 
PPTX
002. Introducere in type script
Dmitrii Stoian
 
PDF
«Typescript: кому нужна строгая типизация?», Григорий Петров, MoscowJS 21
MoscowJS
 
PPTX
TypeScript - Silver Bullet for the Full-stack Developers
Rutenis Turcinas
 
PDF
TypeScript: Angular's Secret Weapon
Laurent Duveau
 
PDF
TypeScriptで快適javascript
AfiruPain NaokiSoga
 
Typescript + Graphql = <3
felixbillon
 
TypeScript: особенности разработки / Александр Майоров (Tutu.ru)
Ontico
 
Typescript tips & tricks
Ori Calvo
 
Power Leveling your TypeScript
Offirmo
 
TypeScript Seminar
Haim Michael
 
TypeScript
GetDev.NET
 
Angular 2 - Typescript
Nathan Krasney
 
TypeScript: Un lenguaje aburrido para programadores torpes y tristes
Micael Gallego
 
Александр Русаков - TypeScript 2 in action
MoscowJS
 
TypeScript for Java Developers
Yakov Fain
 
Typescript
Nikhil Thomas
 
002. Introducere in type script
Dmitrii Stoian
 
«Typescript: кому нужна строгая типизация?», Григорий Петров, MoscowJS 21
MoscowJS
 
TypeScript - Silver Bullet for the Full-stack Developers
Rutenis Turcinas
 
TypeScript: Angular's Secret Weapon
Laurent Duveau
 
TypeScriptで快適javascript
AfiruPain NaokiSoga
 
Ad

Similar to TypeScript: coding JavaScript without the pain (20)

PDF
TypeScript introduction to scalable javascript application
Andrea Paciolla
 
PPTX
Type script - advanced usage and practices
Iwan van der Kleijn
 
PDF
Type script
srinivaskapa1
 
ODP
Getting started with typescript and angular 2
Knoldus Inc.
 
PDF
Using type script to build better apps
ColdFusionConference
 
PDF
Using type script to build better apps
devObjective
 
PDF
Introduction to TypeScript
NexThoughts Technologies
 
PDF
Migrating Web SDK from JS to TS
Grigory Petrov
 
PPTX
TypeScript: Basic Features and Compilation Guide
Nascenia IT
 
PPTX
Complete Notes on Angular 2 and TypeScript
EPAM Systems
 
PDF
Back to the Future with TypeScript
Aleš Najmann
 
PDF
TypeScipt - Get Started
Krishnanand Sivaraj
 
PPTX
Type script
Zunair Shoes
 
PDF
An Introduction to TypeScript
WrapPixel
 
PDF
Reasons to Use Typescript for Your Next Project Over Javascript.pdf
MobMaxime
 
PPTX
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Malla Reddy University
 
PDF
Typescript for the programmers who like javascript
Andrei Sebastian Cîmpean
 
PPTX
Rits Brown Bag - TypeScript
Right IT Services
 
PPT
Learning typescript
Alexandre Marreiros
 
PPTX
Typescript: Beginner to Advanced
Talentica Software
 
TypeScript introduction to scalable javascript application
Andrea Paciolla
 
Type script - advanced usage and practices
Iwan van der Kleijn
 
Type script
srinivaskapa1
 
Getting started with typescript and angular 2
Knoldus Inc.
 
Using type script to build better apps
ColdFusionConference
 
Using type script to build better apps
devObjective
 
Introduction to TypeScript
NexThoughts Technologies
 
Migrating Web SDK from JS to TS
Grigory Petrov
 
TypeScript: Basic Features and Compilation Guide
Nascenia IT
 
Complete Notes on Angular 2 and TypeScript
EPAM Systems
 
Back to the Future with TypeScript
Aleš Najmann
 
TypeScipt - Get Started
Krishnanand Sivaraj
 
Type script
Zunair Shoes
 
An Introduction to TypeScript
WrapPixel
 
Reasons to Use Typescript for Your Next Project Over Javascript.pdf
MobMaxime
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Malla Reddy University
 
Typescript for the programmers who like javascript
Andrei Sebastian Cîmpean
 
Rits Brown Bag - TypeScript
Right IT Services
 
Learning typescript
Alexandre Marreiros
 
Typescript: Beginner to Advanced
Talentica Software
 
Ad

More from Sander Mak (@Sander_Mak) (20)

PDF
Scalable Application Development @ Picnic
Sander Mak (@Sander_Mak)
 
PDF
Coding Your Way to Java 13
Sander Mak (@Sander_Mak)
 
PDF
Coding Your Way to Java 12
Sander Mak (@Sander_Mak)
 
PDF
Java Modularity: the Year After
Sander Mak (@Sander_Mak)
 
PDF
Desiging for Modularity with Java 9
Sander Mak (@Sander_Mak)
 
PDF
Modules or microservices?
Sander Mak (@Sander_Mak)
 
PDF
Migrating to Java 9 Modules
Sander Mak (@Sander_Mak)
 
PDF
Java 9 Modularity in Action
Sander Mak (@Sander_Mak)
 
PDF
Java modularity: life after Java 9
Sander Mak (@Sander_Mak)
 
PDF
Provisioning the IoT
Sander Mak (@Sander_Mak)
 
PDF
Event-sourced architectures with Akka
Sander Mak (@Sander_Mak)
 
PDF
The Ultimate Dependency Manager Shootout (QCon NY 2014)
Sander Mak (@Sander_Mak)
 
PDF
Modular JavaScript
Sander Mak (@Sander_Mak)
 
PDF
Modularity in the Cloud
Sander Mak (@Sander_Mak)
 
PDF
Cross-Build Injection attacks: how safe is your Java build?
Sander Mak (@Sander_Mak)
 
KEY
Scala & Lift (JEEConf 2012)
Sander Mak (@Sander_Mak)
 
KEY
Hibernate Performance Tuning (JEEConf 2012)
Sander Mak (@Sander_Mak)
 
PDF
Akka (BeJUG)
Sander Mak (@Sander_Mak)
 
PDF
Fork Join (BeJUG 2012)
Sander Mak (@Sander_Mak)
 
KEY
Fork/Join for Fun and Profit!
Sander Mak (@Sander_Mak)
 
Scalable Application Development @ Picnic
Sander Mak (@Sander_Mak)
 
Coding Your Way to Java 13
Sander Mak (@Sander_Mak)
 
Coding Your Way to Java 12
Sander Mak (@Sander_Mak)
 
Java Modularity: the Year After
Sander Mak (@Sander_Mak)
 
Desiging for Modularity with Java 9
Sander Mak (@Sander_Mak)
 
Modules or microservices?
Sander Mak (@Sander_Mak)
 
Migrating to Java 9 Modules
Sander Mak (@Sander_Mak)
 
Java 9 Modularity in Action
Sander Mak (@Sander_Mak)
 
Java modularity: life after Java 9
Sander Mak (@Sander_Mak)
 
Provisioning the IoT
Sander Mak (@Sander_Mak)
 
Event-sourced architectures with Akka
Sander Mak (@Sander_Mak)
 
The Ultimate Dependency Manager Shootout (QCon NY 2014)
Sander Mak (@Sander_Mak)
 
Modular JavaScript
Sander Mak (@Sander_Mak)
 
Modularity in the Cloud
Sander Mak (@Sander_Mak)
 
Cross-Build Injection attacks: how safe is your Java build?
Sander Mak (@Sander_Mak)
 
Scala & Lift (JEEConf 2012)
Sander Mak (@Sander_Mak)
 
Hibernate Performance Tuning (JEEConf 2012)
Sander Mak (@Sander_Mak)
 
Fork Join (BeJUG 2012)
Sander Mak (@Sander_Mak)
 
Fork/Join for Fun and Profit!
Sander Mak (@Sander_Mak)
 

Recently uploaded (20)

PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
WYSIWYG Web Builder Crack 2025 – Free Download Full Version with License Key
HyperPc soft
 
PDF
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PDF
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
 
PDF
Rewards and Recognition (2).pdf
ethan Talor
 
PPTX
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
 
PDF
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
 
PDF
2025年 Linux 核心專題: 探討 sched_ext 及機器學習.pdf
Eric Chou
 
PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
PDF
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
PDF
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
PPTX
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
 
PDF
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
WYSIWYG Web Builder Crack 2025 – Free Download Full Version with License Key
HyperPc soft
 
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
 
Rewards and Recognition (2).pdf
ethan Talor
 
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
 
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
 
2025年 Linux 核心專題: 探討 sched_ext 及機器學習.pdf
Eric Chou
 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
 
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
 

TypeScript: coding JavaScript without the pain

  • 1. TypeScript coding JavaScript without the pain @Sander_Mak Luminis Technologies
  • 2. INTRO @Sander_Mak: Senior Software Engineer at Author: Dutch Java Magazine blog @ branchandbound.net Speaker:
  • 3. AGENDA Why TypeScript? Language introduction / live-coding TypeScript and Angular Comparison with TS alternatives Conclusion
  • 4. WHAT'S WRONG WITH JAVASCRIPT? Dynamic typing Lack of modularity Verbose patterns (IIFE)
  • 5. WHAT'S WRONG WITH JAVASCRIPT? Dynamic typing Lack of modularity Verbose patterns (IIFE) In short: JavaScript development scales badly
  • 6. WHAT'S GOOD ABOUT JAVASCRIPT? It's everywhere Huge amount of libraries Flexible
  • 7. WISHLIST Scalable HTML5 clientside development Modular development Easily learnable for Java developers Non-invasive (existing libs, browser support) Long-term vision Clean JS output (exit strategy)
  • 8. WISHLIST Scalable HTML5 clientside development Modular development Easily learnable for Java developers Non-invasive (existing libs, browser support) Long-term vision Clean JS output (exit strategy) ✓✓✓✓✓✓
  • 12. TYPESCRIPT Superset of JavaScript Optionally typed Compiles to ES3/ES5 No special runtime 1.0 in April 2014, future ES6 alignment
  • 13. TYPESCRIPT Superset of JavaScript Optionally typed Compiles to ES3/ES5 No special runtime 1.0 in April 2014, future ES6 alignment In short: Lightweight productivity booster
  • 14. GETTING STARTED $ npm install -g typescript $ mv mycode.js mycode.ts $ tsc mycode.ts
  • 15. GETTING STARTED $ npm install -g typescript $ mv mycode.js mycode.ts $ tsc mycode.ts May even find problems in existing JS!
  • 16. OPTIONAL TYPES Type annotations > var a = 123 > a.trim() ! TypeError: undefined is not a function JS > var a: string = 123 > a.trim() ! Cannot convert 'number' to 'string'. TS runtime compile-time
  • 17. OPTIONAL TYPES Type annotations > var a = 123 > a.trim() ! TypeError: undefined is not a function JS > var a: string = 123 > a.trim() ! Cannot convert 'number' to 'string'. TS Type inference > var a = 123 > a.trim() ! The property 'trim' does not exist on value of type 'number'. Types dissapear at runtime
  • 18. OPTIONAL TYPES Object void boolean integer long short ... String char Type[] any void boolean number string type[]
  • 19. OPTIONAL TYPES Types are structural rather than nominal TypeScript has function types: var find: (elem: string, elems: string[]) => string = function(elem, elems) { .. }
  • 20. OPTIONAL TYPES Types are structural rather than nominal TypeScript has function types: var find: (elem: string, elems: string[]) => string = function(elem, elems) { .. }
  • 21. DEMO: OPTIONAL TYPES code Code: http://bit.ly/tscode
  • 22. INTERFACES interface MyInterface { // Call signature (param: number): string member: number optionalMember?: number myMethod(param: string): void } ! var instance: MyInterface = ... instance(1)
  • 23. INTERFACES Use them to describe data returned in REST calls $.getJSON('user/123').then((user: User) => { showProfile(user.details) }
  • 24. INTERFACES TS interfaces are open-ended: interface JQuery { appendTo(..): .. .. } interface JQuery { draggable(..): .. .. jquery.d.ts } jquery.ui.d.ts
  • 25. OPTIONAL TYPES: ENUMS enum Language { TypeScript, Java, JavaScript } ! var lang = Language.TypeScript var ts = Language[0] ts === "TypeScript" enum Language { TypeScript = 1, Java, JavaScript } ! var ts = Language[1]
  • 26. GOING ALL THE WAY Force explicit typing with noImplicitAny var ambiguousType; ! ambiguousType = 1 ambiguousType = "text" noimplicitany.ts $ tsc --noImplicitAny noimplicitany.ts
  • 27. GOING ALL THE WAY Force explicit typing with noImplicitAny var ambiguousType; ! ambiguousType = 1 ambiguousType = "text" noimplicitany.ts $ tsc --noImplicitAny noimplicitany.ts error TS7005: Variable 'ambiguousType' implicitly has an 'any' type.
  • 28. TYPESCRIPT CLASSES Can implement interfaces Inheritance Instance methods/members Static methods/members Single constructor Default/optional parameters ES6 class syntax similar different
  • 29. DEMO: TYPESCRIPT CLASSES code Code: http://bit.ly/tscode
  • 30. ARROW FUNCTIONS Implicit return No braces for single expression Part of ES6
  • 31. ARROW FUNCTIONS Implicit return No braces for single expression Part of ES6 function(arg1) { return arg1.toLowerCase(); }
  • 32. ARROW FUNCTIONS Implicit return No braces for single expression Part of ES6 function(arg1) { return arg1.toLowerCase(); } (arg1) => arg1.toLowerCase();
  • 33. ARROW FUNCTIONS Implicit return No braces for single expression Part of ES6 function(arg1) { return arg1.toLowerCase(); } (arg1) => arg1.toLowerCase(); Lexically-scoped this (no more 'var that = this')
  • 34. DEMO: ARROW FUNCTIONS code Code: http://bit.ly/tscode
  • 35. TYPE DEFINITIONS How to integrate existing JS code? Ambient declarations Any-type :( Type definitions lib.d.ts Separate compilation: tsc --declaration file.ts
  • 36. TYPE DEFINITIONS DefinitelyTyped.org Community provided .d.ts files for popular JS libs How to integrate existing JS code? Ambient declarations Any-type :( Type definitions lib.d.ts Separate compilation: tsc --declaration file.ts
  • 37. INTERNAL MODULES module StorageModule { export interface Storage { store(content: string): void } ! var privateKey = 'storageKey'; export class LocalStorage implements Storage { store(content: string): void { localStorage.setItem(privateKey, content); } } ! export class DevNullStorage implements Storage { store(content: string): void { } } } ! var storage: StorageModule.Storage = new StorageModule.LocalStorage(); storage.store('testing');
  • 38. INTERNAL MODULES module StorageModule { export interface Storage { store(content: string): void } ! var privateKey = 'storageKey'; export class LocalStorage implements Storage { store(content: string): void { localStorage.setItem(privateKey, content); } } ! export class DevNullStorage implements Storage { store(content: string): void { } } } ! var storage: StorageModule.Storage = new StorageModule.LocalStorage(); storage.store('testing');
  • 39. INTERNAL MODULES module StorageModule { export interface Storage { store(content: string): void } ! var privateKey = 'storageKey'; export class LocalStorage implements Storage { store(content: string): void { localStorage.setItem(privateKey, content); } } ! export class DevNullStorage implements Storage { store(content: string): void { } } } ! var storage: StorageModule.Storage = new StorageModule.LocalStorage(); storage.store('testing');
  • 40. INTERNAL MODULES module StorageModule { export interface Storage { store(content: string): void } ! var privateKey = 'storageKey'; export class LocalStorage implements Storage { store(content: string): void { localStorage.setItem(privateKey, content); } } ! export class DevNullStorage implements Storage { store(content: string): void { } } } ! var storage: StorageModule.Storage = new StorageModule.LocalStorage(); storage.store('testing');
  • 41. INTERNAL MODULES TS internal modules are open-ended: ! module Webshop { export class Cart { .. } } /// <reference path="cart.ts" /> module Webshop { export class Catalog { .. } cart.ts } main.ts
  • 42. INTERNAL MODULES TS internal modules are open-ended: ! module Webshop { export class Cart { .. } } /// <reference path="cart.ts" /> module Webshop { export class Catalog { .. } cart.ts } main.ts Can be hierarchical: module Webshop.Cart.Backend { ... }
  • 43. INTERNAL MODULES TS internal modules are open-ended: ! module Webshop { export class Cart { .. } } /// <reference path="cart.ts" /> module Webshop { export class Catalog { .. } cart.ts } main.ts Can be hierarchical: module Webshop.Cart.Backend { ... } Combine modules: $ tsc --out main.js main.ts
  • 44. DEMO: PUTTING IT ALL TOGETHER code Code: http://bit.ly/tscode
  • 45. EXTERNAL MODULES CommonJS Asynchronous Module Definitions $ tsc --module common main.ts $ tsc --module amd main.ts Combine with module loader
  • 46. EXTERNAL MODULES 'Standards-based', use existing external modules Automatic dependency management Lazy loading AMD verbose without TypeScript Currently not ES6-compatible
  • 47. DEMO + + =
  • 48. DEMO: TYPESCRIPT AND ANGULAR code Code: http://bit.ly/tscode
  • 49. BUILDING TYPESCRIPT $ tsc -watch main.ts grunt-typescript grunt-ts gulp-type (incremental) gulp-tsc
  • 50. TOOLING IntelliJ IDEA WebStorm plugin
  • 51. TYPESCRIPT vs ES6 HARMONY Complete language + runtime overhaul More features: generators, comprehensions, object literals Will take years before widely deployed No typing (possible ES7)
  • 52. TYPESCRIPT vs COFFEESCRIPT Also a compile-to-JS language More syntactic sugar, still dynamically typed JS is not valid CoffeeScript No spec, definitely no Anders Hejlsberg... Future: CS doesn't track ECMAScript 6 !
  • 53. TYPESCRIPT vs DART Dart VM + stdlib (also compile-to-JS) Optionally typed Completely different syntax & semantics than JS JS interop through dart:js library ECMA Dart spec
  • 54. TYPESCRIPT vs CLOSURE COMPILER Google Closure Compiler Pure JS Types in JsDoc comments Less expressive Focus on optimization, dead-code removal
  • 56. CONCLUSION Internal modules Classes/Interfaces Some typing External modules Type defs More typing Generics Type defs -noImplicitAny
  • 57. CONCLUSION TypeScript allows for gradual adoption Internal modules Classes/Interfaces Some typing External modules Type defs More typing Generics Type defs -noImplicitAny
  • 58. CONCLUSION Some downsides: Still need to know some JS quirks Current compiler slowish (faster one in the works) External module syntax not ES6-compatible (yet) Non-MS tooling lagging a bit
  • 59. CONCLUSION High value, low cost improvement over JavaScript Safer and more modular Solid path to ES6
  • 60. MORE TALKS TypeScript: Wednesday, 11:30 AM, same room !Akka & Event-sourcing Wednesday, 8:30 AM, same room @Sander_Mak Luminis Technologies
  • 61. RESOURCES Code: http://bit.ly/tscode ! Learn: www.typescriptlang.org/Handbook @Sander_Mak Luminis Technologies