-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpresentation.html
More file actions
624 lines (433 loc) · 12.3 KB
/
Copy pathpresentation.html
File metadata and controls
624 lines (433 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<!DOCTYPE html>
<html>
<head>
<title>TypeScript for the C# Dev</title>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body { font-family: 'Droid Serif'; }
h1, h2, h3 {
font-family: 'Yanone Kaffeesatz';
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
.remark-slide-content { font-size: 26px; }
.remark-code { font-size: 24px; }
</style>
</head>
<body>
<textarea id="source">
class: center, middle
# TypeScript for the C\# Dev
---
class: center, middle
# Welcome!
???
* Happy to be here this evening to talk with you all about TypeScript
* Thanks to James and Rich for everything that they do
* And thanks to all of the sponsors for all of their support
---
class: center, middle
# Questions
---
class: center, middle
# How confident are you when developing with JavaScript?
---
class: center, middle
# Who's already using TypeScript?
---
class: center, middle
# Who's planning to use TypeScript with Angular (v2+)?
---
class: center, middle
# Who's planning to use TypeScript <br/> with something other than Angular?
---
# Agenda/Goals
* Short introduction to TypeScript
* How to get started using TypeScript
* What problems does (or doesn't) TypeScript solve?
* Do you still need to learn JavaScript?
???
"Will TypeScript solve all of your problems? What exactly is TypeScript and how does it work? How do you get started with using TypeScript? Will it keep you from having to take the time to gain a deep understanding of JavaScript?
In this talk, we'll take a look at how TypeScript can help ease the development of JavaScript-based applications. We'll review what will feel familiar to you as a C# developer, and what might surprise you. And we'll take a look at some of the common mistakes that developers make when learning TypeScript (and by extension, JavaScript)."
---
class: center, middle
# Who's written this JavaScript before?
```
alert('Hello world!');
```
---
class: center, middle
# Or maybe one of these variants?
```
document.write('Hello world!');
```
```
console.log('Hello world!');
```
---
class: center, middle
# Congrats!
## You've written your first TypeScript!
---
class: center, middle
# \#1: TypeScript is a superset of JavaScript
???
The TypeScript core team loves JavaScript!
They're not trying to change JavaScript
They always attempt to follow standards
---
# Question: Which of these function calls is valid?
### `app.js`
```
function add(param1, param2) {
return param1 + param2;
}
const result1 = add(1, 2);
const result2 = add('asdf', 'asdf');
const result3 = add(true, false);
const result4 = add({}, {});
const result5 = add();
const result6 = add(1);
const result7 = add(1, 2, 3);
```
---
# Answer: All of them!
### `app.js`
```
function add(param1, param2) {
return param1 + param2;
}
const result1 = add(1, 2); // 3
const result2 = add('asdf', 'asdf'); // asdfasdf
const result3 = add(true, false); // 1
const result4 = add({}, {}); // [object Object][object Object]
const result5 = add(); // NaN
const result6 = add(1); // NaN
const result7 = add(1, 2, 3); // 3
```
---
# Question: Now which of these function calls is valid?
### `app.ts`
```
function add(param1: number, param2: number) {
return param1 + param2;
}
const result1 = add(1, 2);
const result2 = add('asdf', 'asdf');
const result3 = add(true, false);
const result4 = add({}, {});
const result5 = add();
const result6 = add(1);
const result7 = add(1, 2, 3);
```
---
# Answer: Only the first!
### `app.ts`
```
function add(param1: number, param2: number) {
return param1 + param2;
}
* const result1 = add(1, 2);
const result2 = add('asdf', 'asdf');
const result3 = add(true, false);
const result4 = add({}, {});
const result5 = add();
const result6 = add(1);
const result7 = add(1, 2, 3);
```
---
class: center, middle
# \#2: TypeScript provides [optional] static typing
???
You don't need to use TypeScript's static typing... but everyone does :)
---
# Async/Await
```
async function fetchAsync() {
const response = await fetch('https://api.github.com');
return response.json();
}
fetchAsync()
.then(data => console.log(data))
.catch(error => console.log(error.message));
```
---
# Object Rest and Spread
```
const person = {
firstName: 'Gandalf',
age: 2019,
email: 'graywizard1@hotmail.com'
};
const { firstName, ...other } = person;
// const firstName: string
// const other: { age: number; email: string; }
const newPerson = { firstName, ...other };
// const newPerson: { age: number; email: string; firstName: string; }
```
---
# Decorators
### Angular Component
```
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'angular-cli';
}
```
---
class: center, middle
# \#3: TypeScript enables features from the future
---
class: center, middle
# To recap, TypeScript Is...
### 1) Superset of JavaScript
### 2) Optional static typing
### 3) Features from the future
---
class: center, middle
# Demo: First Look at TypeScript
???
In VS Code...
* VS Code includes the TS language service, but not the TS compiler
* Easiest way to install it is to use NPM
* Add a tsconfig file to your project
* `tsc --init`
* Then build your project using CMD+SHIFT+B
* Select a build task
* You can configure a default build task by running "Configure Default Build Task"
* Lower right hand corner will show you the version that you're using
* Clicking on it will allow you to select a version
* You can use NPM to install a local copy
* `npm init -y`
* `npm install typescript --save-dev`
* Be sure to use `npx tsc` from the command line!
* You can configure VS Code to use a different path for its language service
* Add a `settings.json` file in the `.vscode` folder in the root of your project
* `{ "typescript.tsdk": "node_modules/typescript/lib" }`
https://code.visualstudio.com/docs/languages/typescript
---
# Getting Started in Visual Studio (Proper)
## ASP.NET MVC Projects
In 2017 v15.3...
* Add a TypeScript file (.ts) to your project
* Right click on the project node in Solution Explorer
* Click Properties
* Go to the TypeScript Build tab
* Change TypeScript version to the desired version or "use latest available" to always default to the newest version installed
https://github.com/Microsoft/TypeScript/wiki/Updating-TypeScript-in-Visual-Studio-2017
---
# Getting Started in Visual Studio (Proper)
## ASP.NET Core Projects
* Use NPM to manage dependencies to define tasks
* Use the Visual Studio Task Runner Explorer to run NPM tasks when building your project
* Or edit your project's .csproj file (to not rely upon VS)
```xml
<Target Name="MyPreCompileTarget" BeforeTargets="Build">
<Exec Command="gulp clean" />
</Target>
```
https://docs.microsoft.com/en-us/aspnet/core/client-side/using-gulp?view=aspnetcore-2.1
---
# Other Runtimes, Frameworks, and libraries
* Node
* Use NPM to install TypeScript
* Use NPM to define a task for building your app
* Angular
* Angular CLI
* React
* Create-React-App-TypeScript
* https://github.com/wmonk/create-react-app-typescript
* Vue
* Vue CLI
---
# TypeScript Configuration
* Use `tsc --init` to generate a `tsconfig.json` file
* Common options
* `target`
* `module`
* `allowJs`
* `checkJs`
* `sourceMap`
* `outFile`
* `outDir`
---
# The Promise
* Will TypeScript solve all of my problems?
* Finally... I don't actually have to learn JavaScript!
---
class: center, middle
# Sorry... No
---
# What Problems Does TypeScript Solve?
* Making coding intentions more explicit
* Consistent data typing
* Predictable `null` and `undefined` handling
* Improved developer experience
* Symbol-based navigation
* Code discoverability
---
# What Problems Doesn't TypeScript Solve?
* Runtime behavior
* TypeScript doesn't change the runtime behavior of JavaScript code
* You still need to know JS as a language and the platform that your code is running on
* Lack of richer primitive data types
* JavaScript oddities
---
# How Does TypeScript Help C\# Developers?
* Tooling enhancements
* IntelliSense
* Symbol-based navigation and renaming
* References and Implementations CodeLens
* Auto Imports
* Class extensions
* Private members
* Read only members
* Abstract classes and members
* Interfaces
* Generics
* Non-nullable types
???
* Tooling enhancements
* IntelliSense
* Symbol-based navigation and renaming
* References CodeLens
* `"typescript.referencesCodeLens.enabled": true`
* Implementations CodeLens
* `"typescript.implementationsCodeLens.enabled": true`
* Auto Imports
* Requires TS 2.6+
* Enabled by default
---
# The Danger Zone
## Demo: How can these features give you a false sense of security?
???
* By default, the TS compiler will still generate JS
* Show how to change this via configuration
* Classes
* Not the same as C# classes
* JS class syntax is syntactic sugar over the prototypal inheritance
* Private members
* Are accessible by those writing plain JS
* Abstract classes or members
* Abstract classes can be instantiated via plain JavaScript
---
# Common JavaScript Pitfalls
## What are the common JS pitfalls or misunderstandings for C# devs?
* Data types
* Still just `string`, `number`, and `boolean` primitive data types
* Also `null`, `undefined`, and `Symbol` primitive data types
* And `Object`
* Libraries help fill the gaps
* Comparison operators
* Use `===` and `!==` not `==` and `!=`
* Variable scoping
* Declaring variables with `var` scopes them to the function
* Use `let` and `const` instead of `var`
???
* Data types
* Still just `string`, `number`, and `boolean` primitive data types
* Also `null`, `undefined`, and `Symbol` primitive data types
* And `Object`
* Libraries help fill the gaps
* https://github.com/MikeMcl/decimal.js/
* Comparison operators
* Use `===` and `!==` not `==` and `!=`
* Variable scoping
* Declaring variables with `var` scopes them to the function
* Use `let` and `const` instead of `var`
---
# Comparison Operators
## Some examples from MDN...
```
1 == 1 // true
'1' == 1 // true
1 == '1' // true
0 == false // true
0 == null // false
var object1 = {'key': 'value'}, object2 = {'key': 'value'};
object1 == object2 //false
0 == undefined // false
null == undefined // true
1 != 2 // true
1 != '1' // false
1 != "1" // false
1 != true // false
0 != false // false
```
---
# Variable Scoping
## Using `var`...
```
var i = 100;
for (var i = 0; i < 10; i++) {
console.log(i);
}
console.log(i);
```
---
# Variable Scoping
## Using `const` and `let`...
```
const i = 100;
for (let i = 0; i < 10; i++) {
console.log(i);
}
console.log(i);
```
---
class: center, middle
# Do You Still Need to Learn JavaScript?
---
class: center, middle
# JavaScript for C# Dev Resources
---
# Documentation
* MDN Web Docs
* https://developer.mozilla.org/en-US/
---
# Courses
* JavaScript for C# Developers
* By Shawn Wildermuth
* https://www.pluralsight.com/courses/js4cs
---
# Blog Posts
* JavaScript for C# Developers
* https://www.blewis.io/javascript-for-csharp-developers
* Object-oriented JavaScript for C# Developers
* https://www.barbarianmeetscoding.com/blog/2014/08/20/object-oriented-javascript-for-c-sharp-developers/
* What’s the Difference Between Class & Prototypal Inheritance?
* https://medium.com/javascript-scene/master-the-javascript-interview-what-s-the-difference-between-class-prototypal-inheritance-e4cd0a7562e9
---
class: center, middle
# Should I Always Use TypeScript?
---
class: center, middle
# Questions?
---
class: center, middle
# Thanks!
### James Churchill
### Treehouse: https://teamtreehouse.com/jameschurchill
### Twitter: @SmashDev
### GitHub: smashdevcode
</textarea>
<script src="remark/remark-latest.min.js">
</script>
<script>
var slideshow = remark.create({
ratio: '16:9',
highlightLanguage: 'javascript',
highlightLines: true,
highlightSpans: true,
});
</script>
</body>
</html>