Nick Senger & Jesse van den Kieboom

Similar documents
TypeScript. Types. CS144: Web Applications

An Introduction to TypeScript. Personal Info

Building Your own Widget with ArcGIS API for JavaScript

Deep Dive on How ArcGIS API for JavaScript Widgets Were Built

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

ArcGIS API for JavaScript: Getting Started Andy René

Introduction to TypeScript

Lab 1 - Introduction to Angular

Angular 2 and TypeScript Web Application Development

Angular 4 Syllabus. Module 1: Introduction. Module 2: AngularJS to Angular 4. Module 3: Introduction to Typescript

Stencil: The Time for Vanilla Web Components has Arrived

Advanced Development with the ArcGIS API for JavaScript. Jeremy Bartley, Kelly Hutchins, Derek Swingley

TypeScript coding JavaScript without the pain

Objects and Iterators

Introduction to Programming Using Java (98-388)

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

INF5750. Introduction to JavaScript and Node.js

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Advanced React JS + Redux Development

Advanced Systems Programming

International Research Journal of Engineering and Technology (IRJET) e-issn: Volume: 05 Issue: 06 June p-issn:

ANGULAR 2.X,4.X + TYPESRCIPT by Sindhu

ECMAScript 2015 The Future of JavaScript is Now!

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

LEARN WITH INTRODUCTION TO TYPESCRIPT

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

TypeScript. TypeScript. RxJS

WebStorm, intelligent IDE for JavaScript development

Java Primer 1: Types, Classes and Operators

MAKE NODEJS APIs GREAT WITH TYPESCRIPT

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

IN4MATX 133: User Interface Software

Web Development for Dinosaurs An Introduction to Modern Web Development

Introduction to JavaScript. Evan Caldwell & James Tedrick

Templates and Databinding. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Advanced 3D Features. ArcGIS API for JavaScript. Jesse van den Kieboom, ESRI R&D Center Zürich Thomas Other, ESRI R&D Center Zürich

CLASS DESIGN. Objectives MODULE 4

TypeScript. Language Specification. Version 1.8

CREATE SASSY WEB PARTS. Developer Guide to SASS usage in SPFx

And Even More and More C++ Fundamentals of Computer Science

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

Principles of Programming Languages

Full Stack boot camp

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

JS Event Loop, Promises, Async Await etc. Slava Kim

ECMAScript 2015 and beyond The Future of JavaScript is Now!

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Ten interesting features of Google s Angular Project

Trusted Types - W3C TPAC

JSJS - Project Proposal

Need to Node: Profiling Node.js Applications

Chapter 4 Defining Classes I

Advance Mobile& Web Application development using Angular and Native Script

ArcGIS API for JavaScript: Using Arcade with your Apps. Kristian Ekenes & David Bayer

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Integrating Angular with ASP.NET Core RESTful Services. Dan Wahlin

ArcGIS for Developers: An Introduction. Moey Min Ken

JavaScript for C# Programmers Kevin

Experimental New Directions for JavaScript

Eclipse 4.0. Jochen Krause EclipseSource

September 10,

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

JavaScript Syntax. Web Authoring and Design. Benjamin Kenwright

Chapter 1 - Consuming REST Web Services in Angular

Modular JavaScript. JC Franco. Blake Stearman

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

JavaScript: Sort of a Big Deal,

CMSC 330: Organization of Programming Languages

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

javascripts in the javascripts ffconf 2014 andy wingo

Angular 2 Programming

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Object Oriented Programming. Java-Lecture 11 Polymorphism

Principles of Programming Languages

CS 251 Intermediate Programming Methods and More

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

ArcGIS Runtime: Building Cross-Platform Apps. Rex Hansen Mark Baird Michael Tims Morten Nielsen

Microsoft Visual C# Step by Step. John Sharp

JavaScript Fundamentals_

Javascript. Many examples from Kyle Simpson: Scope and Closures

Another IS-A Relationship

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

JScript Reference. Contents

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Down With JavaScript!

ArcGIS API for JavaScript

Configuring and Customizing the ArcGIS Viewer for Silverlight. Katy Dalton

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Black Hat Webcast Series. C/C++ AppSec in 2014

ArcGIS Online: Developing Web Applications with Geocoding and Routing Services. Deelesh Mandloi Dmitry Kudinov Brad Niemand

ES6 Modules...& Polymer

John Gravois slides:

Welcome. Quick Introductions

Angular 2 and TypeScript Web Application Development

Object Oriented Programming in C#

Lab 1 - Introduction to TypeScript

HTML5 Evolution and Development. Matt Spencer UI & Browser Marketing Manager

Extending Blue Ocean Keith Zantow

Polymorphism. return a.doublevalue() + b.doublevalue();

Transcription:

Using TypeScript with the ArcGIS API for JavaScript Nick Senger & Jesse van den Kieboom Live version of this presentation is available on: https://jkieboom.github.io/devsummit-palm-springs-2018/presentations/typescript-arcgis-js-api

Agenda TypeScript? Fundamentals Advanced types Development tooling & setup Working with the 4.x JS API Accessor, decorators, and advanced concepts

TypeScript?

Superset of JavaScript Transpiles to JavaScript ESNext features (import, =>, rest/spread, async/await, etc) Types Compatible with existing JavaScript

Benefits of TypeScript Easier for multiple people to work on Easier to refactor Easier to test Can help prevent technical debt

Fundamentals

Primitive (Basic) Types boolean, number, string, [], { any type Foo = number; const foo: Foo = 8; const bar: string = "Lorem ipsum"; // Here be dragons const waldo: any = { dostuff: (things: any) => something ;

Type Inference TypeScript compiler can infer types automatically let foo = 8; // number type is inferred foo = 12; // Ok foo = "12"; // Error!

Interfaces Define contracts between parts of an application type Foo = number; type Bar = string; interface Foobar { foo: Foo, bar: Bar const baz: Foobar = { foo: 8, bar: "Lorem ipsum" ; // Ok const qux: Foobar = { foo: "12", bar: "Lorem ipsum" // Error!

interface Foobar { foo: number, bar: string Interfaces facilitate predictable behavior const waldo = { dostuff: (things: Foobar): Foobar => ({ foo: things.foo + 1, bar: `${things.bar!` ) ; waldo.dostuff({ foo: 1, bar: "a" ); // Ok, { foo: 2, bar: "a!" waldo.dostuff(1, "a"); // Error!

Classes class Waldo { public dostuff(things: Foobar): Foobar {... private iteratenumber(num: number) { return num + 1; private addexclamationpoint(str: string) { return `${str!`; const testwaldo = new Waldo(); // Create a Waldo instance testwaldo.iteratenumber(2); // Error!

Extension Interfaces can extend other interfaces or classes Classes can extend other classes and implement interfaces interface Point { x: number; y: number; interface Point3d extends Point { z: number; class MyPoint implements Point3d { x = 0; y = 0; z = 0; class My4dPoint extends MyPoint { time = Date.now();

Advanced types

Union types Type something as one of multiple choices of a type // Set a size as either a number, of a string like "1px", // "2em" etc function setsize(v: number string) { //...

Intersection types Combining multiple types to a single type representing all of the features function mixin<u, T>(u: U, t: T): T & U { //...

Type guards Type guards allow TS to infer a specific type when a value may take multiple types ( union ) Types are narrowed to a more specific set by type guards Builtin type guards like typeof, instanceof or tagged unions function foo(v: number string) { if (typeof v === "number") { // TS infers that v: number return v + 1; else { // TS infers that v: string return `${v + 1`;

Type guards Tagged unions are very useful to discriminate unions of interfaces interface Foo { type: "foo"; foo: string; interface Bar { type: "bar"; bar: string; function func(v: Foo Bar) { if (v.type === "foo") { // TS infers that v: Foo return v.foo; else { // TS infers that v: Bar return v.bar;

Generics Like in C# or Java (not like metaprogramming with templates in C++) "Generalizes" types over type parameters class List<T> { constructor(private data?: T[]) { find(f: (item: T) => boolean): T { //... // Fails const list = new List<number>(["1", "2"]); // OK const list = new List<number>([1, 2]); // TS infers v to be of type number list.find(v => v > 1);

keyof keyof allows for "dynamic" type creation Can help making types flexible but keeping them strict interface Options { shouldquery: boolean; returngeometry: boolean; const defaultoptions: Options = { shouldquery: false, returngeometry: false ; function withoptions(options?: Partial<Options>) { const withdefaults = {...defaultoptions,...options ;

keyof type Partial<T> = { [P in keyof T]?: T[P]; type PartialOptions = Partial<Options>; // // Equivalent to // interface PartialOptions { shouldquery?: boolean; returngeometry?: boolean;

Async/await Makes asynchronous programming easy again (mostly) Internally based on promises Typescript polyfills async/await when targetting ES5 Code

Development tooling

Essentials typescript: npm install --save-dev typescript JS API 4.x typings: npm install --save-dev @types/arcgis-js-api JS API 3.x typings: npm install --save-dev @types/arcgis-jsapi@3

Recommended Visual Studio Code tslint: npm install --save-dev tslint dojo typings: npm install --save-dev dojo-typings

Setting Up developers.arcgis.com/javascript/latest/guide/typescript-setup

Working with the API

Imports JS API is currently strictly AMD Conventionally classes are exported directly Requires the use of require style imports import MapView = require("esri/views/mapview") Or, use esmoduleinterop with typescript 2.7.2

Auto-cast Due to nature of types, auto-cast does not type-check get and set must have the same type Auto-casting is supported in constructor signatures only Still helps in lots of cases For setting properties, need to import the relevant modules

Typing improvements Use of generics where possible Collection<T> Strictly type events ( MapView.on("mouse-wheel",...) )) "Advanced" auto-casts like colors ( "red" ), screen sizes ( "5px" ) and basemaps "streets"

Advanced API concepts

Promises In 4.7, promises are more compatible with native promises Replaced then with when for esri/core/promise Typings are more compatible (although not fully compatible) General advice is to wrap API promises in native if needed until JS API switches to native promises

Writing Accessor based classes Can be useful to use Accessor based classes in your app Also required for creating custom API based widgets API classes are using dojo declare, requires some additional work to integrate with TS Code

Multiple inheritance Multiple inheritance possible with dojo declare Supported in typescript at runtime and strictly type-checked Uses declaration merging Code

Extending the API typings API typings are not always as strict as they can be In rare occasions typings are missing or imprecise Typings can be externally "patched" through declaration merging Code

Questions? Help us to improve by filling out the survey Slides & Demos: github.com/nicksenger/2018-ts-ds