forked from basarat/TypeScriptDeepDive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1289 lines (1145 loc) · 29.1 KB
/
index.html
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>TypeScript Deep Dive</title>
<meta name="description" content="Typescript Education">
<meta name="author" content="Basarat Ali Syed">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/source/skybold.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section class="git" data-state="git">
<h1>TypeScript</h1>
<h2>Deep Dive</h2>
<p>Specification * Recommendations * Patterns</p>
<br/>
<p>Press Space neo</p>
<br/>
<p>
<small>Originally by <a href="http://basarat.com">BAS (Basarat Ali Syed)</a> / <a href="http://twitter.com/basarat">@basarat</a></small>
</p>
</section>
<section>
<h2>What does typescript provide?</h2>
<ul>
<li>Strong Typing</li>
<li>Better Syntax</li>
</ul>
<aside class="notes">
The two things typescript provides
</aside>
</section>
<!-- Example of nested vertical slides -->
<section>
<section>
<markdown>
Strong Typing
---
- Interfaces
- Inline typing (type declarations)
- Ambients
- Variable
- Functions
- Classes
- Modules
</markdown>
</section>
</section>
<section>
<section><h1>Interfaces</h1> What do you want from me?</section>
<section>
<h2>Interfaces</h2>
<codeseg>
interface fooInterface{
// constructor
new (fooParam1:number,fooParam2?):number;
// call signature callable without new
(fooParam1:any):string;
// indexable
[index:string]:number;
}
</codeseg>
</section>
<section>
<h2>Interfaces</h2>
<codeseg>
interface fooInterface{
// functions with overloads
fooFunc1(fooParam1:bool):any;
fooFunc2(...fooParam1:number[]):any;
fooFunc3:(fooParam1:bool)=>any;
// variables
fooVar1:number;
fooVar2?:Array;
// array
fooArr1:{
[index:string]:number;
};
fooArr2:number[];
}
</codeseg>
</section>
<section>
<h2>Open Ended</h2>
<codeseg>
interface foo{
x:number;
}
interface foo{
y:number;
}
var bar:foo = {
x:123,
y:123
}
</codeseg>
</section>
</section>
<section>
<section>
<h1>Declaration</h1>What is that?
</section>
<section>
<h2>Type Annotation</h2>
<codeseg>
var x:number;
var y:string;
var z:number[];
var foo:{a:any;b:()=>any;} // powerful inline declaration
<codeseg>
</section>
<section>
<h2>inline declarations</h2>
Offer the complete Interface syntax but inline. Syntax:
<codeseg>
var __varname__: {
__membername__ : __membertype__ ;
__membername__ : __membertype__ ;
// Repeat
}
</codeseg>
e.g:
<codeseg>
var x:{
() : Function;
[foo:string]: number;
new() : string;
}
</codeseg>
</section>
<section>
<h2>Function signature</h2>
<codeseg>
// interface / inline interface implementation
interface fooInterface{
simpleSyntax():void;
lambdaSyntax:()=>void;
}
var fooInline:{
simpleSyntax():void;
lambdaSyntax:()=>void;
}
</codeseg>
Lambda signature is required for function signatures in vars / parameter type declarations.
</section>
<section>
<h2>Function signature for callables</h2>
A bit inconsistent.
<codeseg>
// Simple function, When declaring a var or function arguments
var x1 : (s: string)=>string;
// When saying something is callable
var x2 : { (s: string): string; }
interface x3{
(s:string):string;
}
// Errors:
var y1 : (s: string):string;
var y2: { (s: string)=> string; }
interface y3{
(s:string)=>string;
}
</codeseg>
</section>
<section>
<h2>Type Inference</h2>
<codeseg>
var x = 123;
var y;
y = 123;
x = "asdf";
y = "asdf";
var z={foo:123}
</codeseg>
What type is y?
Does the compiler give an error in this code segment?
</section>
<section>
<h2>any</h2>
<codeseg>
var x:any;
var y:number = x;
x = y;
</codeseg>
Can be assigned to anything. Can be assigned anything.
</section>
<section>
<h2>null, undefined</h2>
Typescript: For compiler (not runtime) both are type any.
<codeseg>
var x = null;
var y = undefined;
</codeseg>
Javascript: null is an object instance to mean nothing. undefined is a type as well as a value for something not initialized.
<codeseg>
var TestVar;
alert(TestVar); //shows undefined
alert(typeof TestVar); //shows undefined
var TestVar = null;
alert(TestVar); //shows null
alert(typeof TestVar); //shows object
</codeseg>
</section>
<section>
<h2>Which string is that?</h2>
<codeseg>
var obj:String = "123";
var primitive:string = "123";
obj=primitive; // exactly what the first line is doing
primitive=obj; // Error
</codeseg>
Recommendation: Just use string.
</section>
</section>
<section>
<section><h1>Ambients</h1>I thought I saw a pussy cat</section>
<section>
<h2>Ambients</h2>
<markdown>The magical keyword is ***declare***.
Generate no code whatsoever so obviously
- no variable initializer
- no function bodies / no default parameters</markdown>
</section>
<section>
<h2>Ambients</h2>
<codeseg>
declare var angular;
declare function foo(takes:string):number;
declare class Fancy{
notMuch:string;
}
</codeseg>
</section>
</section>
<section>
<section>
<h2>Better Syntax</h2>
<markdown>- Function signatures
- Optional parameters
- Rest parameters
- Default parameters
- Arrow Functions
- Classes
- super
- public / private
- Modules
- Internal
- AMD / CommonJS</markdown>
</section>
</section>
<section>
<section>
<h1>Functions</h1>
are actually functions
</section>
<section>
<h2> Optional </h2>
??????
<codeseg>
function foo(req:string,optional?:number){}
</codeseg>
</section>
<section>
<h2>Default</h2>
<p>Effectively optional. Can even do expressions</p>
<codeseg>
function foo( req:string, def:number=3, optional=req+def ) { }
</codeseg>
Generates:
<codeseg>
function foo(req, def, optional) {
if (typeof def === "undefined") { def = 3; }
if (typeof optional === "undefined") { optional = req + def; }
}
</codeseg>
</section>
<section>
<h2>Rest</h2>
<markdown>
params denoted by ...
- explicitly type declared
- have to be last
</markdown>
<codeseg>
function foo(req:string,...blabla:any[]){}
</codeseg>
becomes
<codeseg>
function foo(req) {
var blabla = [];
for (var _i = 0; _i < (arguments.length - 1); _i++) {
blabla[_i] = arguments[_i + 1];
}
}
</codeseg>
</section>
<section>
<h2>Function overloading</h2>
<codeseg>
interface IFoo{
test(x:string);
test(x:number);
}
</codeseg>
</section>
<section>
<h2>Function overloading</h2>
The actual function should be able to accept any of the overload members.
<codeseg>
class Foo implements IFoo {
test(x: string);
test(x: number);
test(x: any) {
if (typeof x === "string") {
//string code
} else {
//number code
}
}
}
</codeseg>
The overloads go through the same code generation restrictions e.g you cannot define default values etc. Also the final signature is not directly callable from typescript since its generally an over generalized implementation (any etc).
</section>
<section>
<h2>Function overloading</h2>
How to handle variable parameters
<codeseg>
function f():number; // Error
function f(x:any):any{
}
function g():number; // Okay
function g(x?:any):any{
}
</codeseg>
Don't need to be in a class.
</section>
<section>
<h2>Function Instances</h2>
<codeseg>
// global or module
function simpleSyntax():void{}
var lambdaSyntax1=():void=>{}
// class , basically remove function or var
class fooClass{
simpleSyntax():void{}
lambdaSyntax=():void=>{}
}
// object literal
var fooObject={
simpleSytax: function():void{},
lambdaSyntax: ():void=>{}
}
// Reminder the lambda based signature was a bit different:
// lambdaSyntax:()=>void;
</codeseg>
</section>
</section>
<section>
<section>
<h1>Class</h1>
Now called class
</section>
<section>
<h2>Basic</h2>
<codeseg>
class Foo{
public member:number;
static stat:number = 123;
constructor(){this.member = 123;}
func(){
this.member = 256;
}
}
</codeseg>
Note: to access members you ALWAYS have to use this
</section>
<section>
<h2>Basic</h2>
<codeseg>
var Foo = (function () {
function Foo() {
this.member = 123;
}
Foo.stat = 123;
Foo.prototype.func = function () {
this.member = 256;
};
return Foo;
})();
</codeseg>
</section>
<section>
<h2>Basic</h2>
<codeseg>
class Foo{
constructor(public member){this.member = 123;}
func(){
this.member = 256;
}
}
</codeseg>
</section>
<section>
<h2>Cool: Member not generated</h2>
<codeseg>
class Foo{
public member:number;
}
</codeseg>
becomes:
<codeseg>
var Foo = (function () {
function Foo() { }
return Foo;
})();
</codeseg>
</section>
<section>
<h2>Interface implementation</h2>
Primarily for the person implementing the class. The class can be used in place of the interface even if it doesn't explictly implement that interface.
<codeseg>
interface iFoo{
x:number;
}
class Foo1 implements iFoo{
x:number;
}
class Foo2{
x:number;
}
var la:iFoo;
la = new Foo1();
la = new Foo2();
</codeseg>
</section>
<section id="classlimits">
<h2>Syntax Limitations</h2>
<p>
Not everthing that can be declared in an interface can be implemented by a Typescript class e.g. Indexible, call signatures
</p>
<codeseg>
interface WidgetMap {
[name: string]: Widget;
}
var map: WidgetMap = {};
map['gear'] = new GearWidget();
var w = map['gear']; // w is inferred to type Widget
</codeseg>
</section>
<section>
<h2>Syntax Limitations</h2>
<p>
Callable example
</p>
<codeseg>
interface Foo {
(): any;
(value: any): void;
}
function createFoo(): Foo {
var getFunc = () => { console.log("get"); return "foo"; }
var setFunc = (value: any) => { console.log("set"); }
return (value?: any) => {
if (value) {
setFunc(value);
} else {
return getFunc();
}
}
}
var f = createFoo();
f("bar");
f();
</codeseg>
</section>
<section>
<h2>Optional members?</h2>
<codeseg>
interface foo{
x?:number;
}
class boo1 implements foo{
}
class boo2 implements foo{
x:number;
}
</codeseg>
</section>
</section>
<section>
<section id="super">
<h1>Class Inheritance</h1>
<strong>super</strong> : Not the Australian kind.
</section>
<section>
<h2>Basic</h2>
<codeseg>
class FooBase{
}
class FooChild extends FooBase{
}
</codeseg>
</section>
<section>
<h2>Basic</h2>
<codeseg>
var __extends = this.__extends || function (d, b) {
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var FooBase = (function () {
function FooBase() { }
return FooBase;
})();
var FooChild = (function (_super) {
__extends(FooChild, _super);
function FooChild() {
_super.apply(this, arguments);
}
return FooChild;
})(FooBase);
</codeseg>
</section>
<section>
<h2>Static is Broken</h2>
<codeseg>
var __extends = this.__extends || function (d, b) {
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
</codeseg>
vs.
<codeseg>
var __extends = this.__extends || function(d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function f() { this.constructor = d; }
f.prototype = b.prototype;
d.prototype = new f();
}
</codeseg>
<a href="http://typescript.codeplex.com/workitem/825">work item</a>
</section>
<section>
<h2>super</h2>
<codeseg>
class base {
test(foo:number){
console.log(foo);
}
}
class child extends base{
constructor(public x:number){
super();
super.test(x); // 15
this.test(x); // 25
}
test(foo:number){
console.log(foo+10);
}
}
var test=new child(15);
</codeseg>
</section>
<section>
<h2>super</h2>
super called via call<br/>super (dot) goes directly to prototype
<codeseg>
function child(x) {
_super.call(this);
this.x = x;
_super.prototype.test.call(this, x);
this.test(x);
}
</codeseg>
</section>
<section>
<h2>Super Cautious</h2>
<codeseg>
class A{
fooMem=10;
}
class B extends A{
constructor(){
console.log(this.fooMem); // undefined
super();
console.log(this.fooMem);
console.log(super.fooMem); // undefined
}
};
var test = new B();
</codeseg>
Make super first call and only use for function access afterwords.
</section>
</section>
<section>
<section id="this"><h1>this</h1> which one? </section>
<section>
<h2>Save me</h2>
<markdown>
Typescript knows. But doesn't mean it will save you.
- Constructor / member functions
- *this* a type of containing class
- Static functions
- *this* is of type constructor function
- All other places its any
</markdown>
</section>
<section>
<h2>Err</h2>
Typescript will complain if you miss this. It will say member not defined. So novice way is to put in this.
<codeseg>
class Example{
member = 10;
constructor(){
// Could have been an jquery call
// you were porting from js
setTimeout(function() {
alert(this.member);
},100);
}
}
var ex = new Example();
</codeseg>
But this code is wrong since "this" is any and not the class instance.
</section>
<section>
<h2>Arrow Functions</h2>
<codeseg>
// all of these are the same
var test1=(x:number)=>{return x*10};
var test2=(x:number)=>(x*10);
var test3=(x:number)=>x*10;
var test4=(x)=>x*10;
var test5=x=>x*10;
// {} require a return statement
// This is actually returning undefined
// which is a valid number and fails silently
var test6=(x:number)=>{x*10};
</codeseg>
</section>
<section>
<h2>Arrow Functions</h2>
For lexical scoping.
<codeseg>
var foo = () => {return this;}
</codeseg>
generates:
<codeseg>
var _this = this;
var foo = function () {
return _this;
};
</codeseg>
</section>
<section>
<h2>Err Fix</h2>
<codeseg>
class Example{
member = 10;
constructor(){
// Could have been an jquery call
// you were porting from js
setTimeout(()=>{
alert(this.member);
},100);
}
}
var ex = new Example();
</section>
<section>
<h2>So basically</h2>
Replace all functions in arguments with ()=>
</section>
<section>
<h2>Almost</h2>
<markdown>
Keep functions for:
- When you want this to be any. E.g. $.each.
- When you need both. Then resort to the manual closure trick of var self = this;
</markdown>
</section>
<section>
<h2>Pattern</h2>
When your class is just a collection of functions other people would call.
<codeseg>
class Example{
func1:(number)=>void;
func2:()=>number;
member:number = 10;
constructor(){
this.func1=(x)=>{this.member=x};
this.func2=()=>this.member;
}
}
</codeseg>
The body needs to seperate from declaration since this is only available in constructor / member functions.
<br><br>Useful for libs like knockout.
</section>
</section>
<section>
<section><h1>Internal Module</h1>Fetch boy!</section>
<section>
<h2>Internal Modules</h2>
Like namespaces in C# but it is actually a singleton instance.
<codeseg>
module M{
var s = "test"
export function f(){
return s;
}
}
</codeseg>
Just the <em>export</em> keyword
</section>
<section>
<h2>Internal Modules</h2>
The variables inside the variables are only available to the current body of the module via closure.
<codeseg>
// Generated
var M;
(function (M) {
var s = "test";
function f() {
return s;
}
M.f = f;
})(M || (M = {}));
</codeseg>
I say current body since modules are also open ended and can therefore have more than one bodies.
</section>
<section>
<h2>Internal Modules</h2>
<codeseg>
module M{
export var s = "test"
}
module M{
export var n = 123;
}
</codeseg>
generates:
<codeseg>
var M;
(function (M) {
M.s = "test";
})(M || (M = {}));
var M;
(function (M) {
M.n = 123;
})(M || (M = {}));
</codeseg>
</section>
<section>
<h2>Quick nesting</h2>
<codeseg>
module A.B.C{
}
// same as
module A{
export module B{
export module C{
}
}
}
</codeseg>
And you can have this side by side as modules are open ended.
</section>
<section>
<h2>Aliasing</h2>
<codeseg>
module x.y.z{
export var n =10;
}
// Alias
import foo = x.y.z;
console.log(foo.n);
</codeseg>
generates:
<codeseg>
var foo = x.y.z;
console.log(foo.n);
</codeseg>
</section>
<section>
<h2>Careful with naming</h2>
<codeseg>
module Test{
export class Test{
}
}
</codeseg>
<codeseg>
var Test;
(function (Test) {
var Test = (function () {
function Test() { }
return Test;
})();
Test.Test = Test;
})(Test || (Test = {}));
</codeseg>
Tip: Do not reuse the module name inside the module
</section>
<section id="declarationspaces">
<h2>Declaration Spaces</h2>
<markdown>
- Types are contributed by interface , module , class.
- Class and module also contribute a variable (constructor for class , instance for module)
</markdown>
</section>
<section>
<h2>Declaration Spaces</h2>
"The name does not exist in the current scope".
<codeseg>
module M
{
export interface P {}
}
import im = M;
var foo1:im.P; // Okay
var vm = M;
var foo2:vm.P; // Error
</codeseg>
You will still get autocomplete.
</section>
</section>
<section>
<section><h1>Structural Typing</h1>Perhaps I can give you something else?</section>
<section>
<h2>Class have their own brand</h2>
<codeseg>
interface iFoo{
x:number;
}
class Foo1 implements iFoo{
x:number;
}
class Foo2{
x:number;
}
var la:iFoo;
la = new Foo1();
la = new Foo2();
var fa:Foo1 = new Foo2(); //Error
</codeseg>
</section>
<section>
<h2>That brand is too mainstream</h2>
Everything else <b>modules</b>, <b>interfaces</b>, <b>inline</b> declarations and <b>inferred</b> structures are behaviour free.
<codeseg>
interface inter{
foo:number;
}
module mod{
export var foo:number;
}
var w:inter;
var x:mod;
var y:{foo:number;}
var z={foo:123}
w=x=y=z;
z=y=x=w;
</codeseg>
</section>
<section>
<h2>Its okay to have more</h2>
<codeseg>
interface inter{
foo:number;
}
var x:inter;