Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is...

27
Using TypeScript Using TypeScript with ArcGIS API for JavaScript with ArcGIS API for JavaScript Noah Sager | René Rubalcava slides: https://git.io/fj2Ej 1

Transcript of Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is...

Page 1: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Using TypeScriptUsing TypeScriptwith ArcGIS API for JavaScriptwith ArcGIS API for JavaScript

Noah Sager | René Rubalcava

slides: https://git.io/fj2Ej

1

Page 2: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

AgendaAgenda

What is TypeScript?Why use TypeScript?Setup and First stepsLive Action DemoWhere can I get more info?

2

Page 3: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

What is TypeScript?What is TypeScript?

3

Page 4: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Where do I begin?Where do I begin?

4

Page 6: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Why use TypeScript?Why use TypeScript?

TypeScript adds type support to JavaScript

6

Page 7: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Why use TypeScript?Why use TypeScript?

Enhanced IDE support

7

Page 8: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Why use TypeScript?Why use TypeScript?

Makes use of the latest JavaScript features

8

Page 9: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Why use TypeScript?Why use TypeScript?

Makes use of the latest JavaScript features

9

Page 10: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Setup and First stepsSetup and First steps

1. The recommended way to install TypeScript is via node and npm.

2. Make sure to install TypeScript globally:

3. Install the ArcGIS API for JavaScript Typings:

npm install -g typescriptnpm install -g typescript

npm install --save @types/arcgis-js-apinpm install --save @types/arcgis-js-api

10

Page 11: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Demo: Build a TypeScript app from scratchDemo: Build a TypeScript app from scratch

11

Page 12: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Tip!Tip!ArcGIS API for JavaScript Snippets

12

Page 13: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Demo Steps:Demo Steps:mkdir ts-demo && cd ts-demomkdir app && mkdir cssnpm init --yes && tsc --initnpm i -D @types/arcgis-js-api

13

Page 14: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

index.htmlindex.html

!getApi

Snippet shortcuts

<<bodybody>>

<<divdiv idid==""viewDivviewDiv"">></</divdiv>>

<<scriptscript>>

requirerequire(([["app/main""app/main"]]));;

</</scriptscript>>

</</bodybody>>

14

Page 15: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

tscon�g.jsontscon�g.json{{

"compilerOptions": { "compilerOptions": {

"lib": ["dom", "es2015.promise", "es5"], "lib": ["dom", "es2015.promise", "es5"],

"module": "amd", // output files as AMD modules "module": "amd", // output files as AMD modules

"sourceMap": true, "sourceMap": true,

"target": "es5", "target": "es5",

"noImplicitAny": true, "noImplicitAny": true,

"suppressImplicitAnyIndexErrors": true, "suppressImplicitAnyIndexErrors": true,

"esModuleInterop": true "esModuleInterop": true

} }

}}

15

Page 16: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

css/main.csscss/main.css

and add it to html

html,html,

body,body,

#viewDiv#viewDiv {{

paddingpadding:: 0 0;;

marginmargin:: 0 0;;

heightheight:: 100% 100%;;

widthwidth:: 100% 100%;;

}}

<<linklink relrel==""stylesheetstylesheet"" hrefhref==""css/main.csscss/main.css"">>

16

Page 17: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

app/main.tsapp/main.tsimports

importimport WebMap WebMap fromfrom "esri/WebMap""esri/WebMap";;

importimport MapView MapView fromfrom "esri/views/MapView""esri/views/MapView";;

importimport LayerList LayerList fromfrom "esri/widgets/LayerList""esri/widgets/LayerList";;

importimport esri esri == __esri __esri;;

17

Page 18: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

app/main.tsapp/main.tsWebMap and MapView

constconst map map == newnew WebMapWebMap(({{

portalItem portalItem:: {{

id id:: "d5dda743788a4b0688fe48f43ae7beb9""d5dda743788a4b0688fe48f43ae7beb9"

}}

}}));;

// Add the map to a MapView// Add the map to a MapView

constconst view view == newnew MapViewMapView(({{

container container:: "viewDiv""viewDiv",,

map map

}}));;

18

Page 19: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

app/main.tsapp/main.tsLayerList

// Add a legend instance to the panel of a// Add a legend instance to the panel of a

// ListItem in a LayerList instance// ListItem in a LayerList instance

constconst layerList layerList == newnew LayerListLayerList(({{

view view,,

listItemCreatedFunctionlistItemCreatedFunction:: eventevent =>=> {{

constconst item item:: esri esri..ListItem ListItem == event event..itemitem;;

ifif ((itemitem..layerlayer..typetype !=!= "group""group")) {{

item item..panel panel == {{

content content:: "legend""legend",,

open open:: truetrue

}} asas esri esri..ListItemPanelListItemPanel;;

}}

}

19

Page 20: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

start typescript compiler

tsc -wtsc -w

20

Page 21: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Tip: Hide .js and .jsmap �lesTip: Hide .js and .jsmap �lesReduce clutterVSCode: Add below to user preferences in �les.exclude

"**/*.js.map": true, "**/*.js.map": true,

"**/*.js": { "**/*.js": {

"when": "$(basename).ts" "when": "$(basename).ts"

21

Page 22: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Tip: Debugging with source mapsTip: Debugging with source mapsEnable source maps in browser dev tools

Set breakpoints in .ts instead of .js

22

Page 23: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Tip: Use __esri instead of importTip: Use __esri instead of importOnly contains type interfacesCan use when not instantiating type

importimport esri esri == __esri __esri;;

constconst layerList layerList == newnew LayerListLayerList(({{

view view,,

listItemCreatedFunctionlistItemCreatedFunction:: eventevent =>=> {{

constconst item item == event event..item item asas esri esri..ListItemListItem;;

}}

}}));;

23

Page 24: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

Where can I get more info?Where can I get more info?SDK DocumentationEsri-related training and webinarsArcGIS BlogsGeoNet, StackExchange, Spatial Community in Slack, etc.

24

Page 25: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

25

Page 26: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

26

Page 27: Using TypeScript with ArcGIS API for JavaScript · 1. The recommended way to install TypeScript is via node and npm. 2. Make sure to install TypeScript globally: 3. Install the ArcG

27