TypeScript coding JavaScript without the pain

Similar documents
One language to rule them all: TypeScript. Gil Fink CEO and Senior Consultant, sparxys

55249: Developing with the SharePoint Framework Duration: 05 days

"Charting the Course... MOC A: Developing with the SharePoint Framework. Course Summary

Front End. Presentation Layer. UI (User Interface) User <==> Data access layer

Advanced React JS + Redux Development

Introduction to TypeScript

JavaScript: Sort of a Big Deal,

TypeScript. Types. CS144: Web Applications

Angular 2 and TypeScript Web Application Development

JavaScript CS 4640 Programming Languages for Web Applications

Welcome. Quick Introductions

Frontend UI Training. Whats App :

An Introduction to TypeScript. Personal Info

JavaScript CS 4640 Programming Languages for Web Applications

Introduction to the SharePoint Framework Bob German Principal Architect - BlueMetal. An Insight company

INF5750. Introduction to JavaScript and Node.js

TypeScript. Language Specification. Version 1.8

and why you should care but probably don t

Sample Copy. Not For Distribution.

Experimental New Directions for JavaScript

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

JavaScript Lecture 1

JavaScript: Introduction, Types

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Advance Mobile& Web Application development using Angular and Native Script

Comprehensive AngularJS Programming (5 Days)

Treating Framework Fatigue With JavaScript

IN4MATX 133: User Interface Software

Angular 2 Programming

Nick Senger & Jesse van den Kieboom

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Web Development for Dinosaurs An Introduction to Modern Web Development

JSJS - Project Proposal

Scala, Your Next Programming Language

Arjen de Blok. Senior Technical Consultant bij ICT Groep ( sinds 1995 Programmeren sinds 1990 Technologiën. Links

RequireJS Javascript Modules for the Browser. By Ben Keith Quoin, Inc.

LEARN WITH INTRODUCTION TO TYPESCRIPT

Code verification. CSE 331 University of Washington. Michael Ernst

ES6 Modules...& Polymer

Dart. Gilad Bracha for the Dart Team

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

"Charting the Course... Comprehensive Angular. Course Summary

Stencil: The Time for Vanilla Web Components has Arrived

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

TYPESCRIPT. Presented by Clarke Bowers

Java Primer 1: Types, Classes and Operators

Javascript. Many examples from Kyle Simpson: Scope and Closures

Modern frontend workflows in Liferay Portal and Liferay DXP. Iván Zaera Avellon, Liferay Chema Balsas, Liferay Pier Paolo Ramon, SMC

Building Your own Widget with ArcGIS API for JavaScript

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

Angular 2 and TypeScript Web Application Development

Modular JavaScript. JC Franco. Blake Stearman

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Principles of Programming Languages

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to Programming Using Java (98-388)

JavaScript Rd2. -Kyle Simpson, You Don t Know JS

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

Moodle JavaScript. Using AMD with RequireJS. Daniel Thee Roperto.

CS240: Programming in C

CS312: Programming Languages. Lecture 21: JavaScript

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

Type Checking. Error Checking

Lab 1 - Introduction to Angular

Modern JavaScript: The Smalltalk Influence

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

WebStorm, intelligent IDE for JavaScript development

Semantic Analysis. Compiler Architecture

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Introduce C# as Object Oriented programming language. Explain, tokens,

Deep Dive on How ArcGIS API for JavaScript Widgets Were Built

Presented by. Dheepa Iyer Managing Consultant. Commissioned for. Reston SharePoint User Group. SharePoint Framework. May 2017

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

JavaScript & Security get married. Yan Zhu NCC Group SF Open Forum 9/17/15

CMSC 330: Organization of Programming Languages

Dart is an open-source general-purpose programming language. It is originally developed by Google and later approved as a standard by ECMA.

Frontend II: Javascript and DOM Programming. Wednesday, January 7, 15

ECMAScript 2015 The Future of JavaScript is Now!

Principles of Programming Languages

Decoupled Drupal with Angular

CGS 3066: Spring 2015 JavaScript Reference

React(.js) the Domino Way High-Performance Client for Domino. Knut Herrmann

ClojureScript. The Essence of Alchemy

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Indiana Nimphius and the JavaScript Temple of Doom

CSE 307: Principles of Programming Languages

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

webpack bundle inner structure and optimization Alexey Ivanov, Evil Martians

G Programming Languages - Fall 2012

Modern and Responsive Mobile-enabled Web Applications

Web UI. Survey of Front End Technologies. Web Challenges and Constraints. Desktop and mobile devices. Highly variable runtime environment

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Working with JavaScript

npm install [<name> [<name>...]] [--save --save-dev --save-optional]

CS558 Programming Languages

Analysis of Security Critical APIs

Transcription:

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)

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() JS! TypeError: undefined is not a function runtime > var a: string = 123 > a.trim() TS! Cannot convert 'number' to 'string'. compile-time

OPTIONAL TYPES Type annotations Type inference > var a = 123 > a.trim()! TypeError: undefined is not a function JS > var a = 123 > a.trim()! The property 'trim' does not exist on value of type 'number'. > var a: string = 123 > a.trim() TS! Types dissapear at runtime Cannot convert 'number' to 'string'.

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 similar Inheritance Instance methods/members Static methods/members Single constructor Default/optional parameters ES6 class syntax 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 How to integrate existing JS code? DefinitelyTyped.org Community provided.d.ts files for popular JS libs 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: Combine modules: module Webshop.Cart.Backend { $ tsc --out main.js main.ts...

DEMO: PUTTING IT ALL TOGETHER code Code: http://bit.ly/tscode

EXTERNAL MODULES CommonJS $ tsc --module common main.ts Asynchronous $ tsc --module amd main.ts Module Definitions 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