-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtypescript.html
More file actions
919 lines (760 loc) · 52.2 KB
/
typescript.html
File metadata and controls
919 lines (760 loc) · 52.2 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TypeScript Mastery - Better Dev</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header class="topbar">
<button class="sidebar-toggle" aria-label="Open navigation" aria-expanded="false">
<span class="hamburger-icon"></span>
</button>
<a href="index.html" class="logo">Better Dev</a>
</header>
<div class="sidebar-backdrop" aria-hidden="true"></div>
<aside class="sidebar" aria-label="Site navigation">
<div class="sidebar-header">
<span class="sidebar-title">Navigation</span>
<button class="sidebar-close" aria-label="Close navigation">×</button>
</div>
<div class="sidebar-search">
<input type="text" class="sidebar-search-input" placeholder="Search topics..." aria-label="Search topics">
<div class="sidebar-search-results"></div>
</div>
<nav class="sidebar-nav">
<div class="sidebar-group">
<a href="index.html">Home</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Mathematics</div>
<a href="pre-algebra.html">Pre-Algebra</a>
<a href="algebra.html">Algebra</a>
<a href="sequences-series.html">Sequences & Series</a>
<a href="geometry.html">Geometry</a>
<a href="calculus.html">Calculus</a>
<a href="discrete-math.html">Discrete Math</a>
<a href="linear-algebra.html">Linear Algebra</a>
<a href="probability.html">Probability & Statistics</a>
<a href="binary-systems.html">Binary & Number Systems</a>
<a href="number-theory.html">Number Theory for CP</a>
<a href="computational-geometry.html">Computational Geometry</a>
<a href="game-theory.html">Game Theory</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Data Structures & Algorithms</div>
<a href="dsa-foundations.html">DSA Foundations</a>
<a href="arrays.html">Arrays & Strings</a>
<a href="stacks-queues.html">Stacks & Queues</a>
<a href="hashmaps.html">Hash Maps & Sets</a>
<a href="linked-lists.html">Linked Lists</a>
<a href="trees.html">Trees & BST</a>
<a href="graphs.html">Graphs</a>
<a href="sorting.html">Sorting & Searching</a>
<a href="patterns.html">LeetCode Patterns</a>
<a href="dp.html">Dynamic Programming</a>
<a href="advanced.html">Advanced Topics</a>
<a href="string-algorithms.html">String Algorithms</a>
<a href="advanced-graphs.html">Advanced Graphs</a>
<a href="advanced-dp.html">Advanced DP</a>
<a href="advanced-ds.html">Advanced Data Structures</a>
<a href="leetcode-650.html">The 650 Problems</a>
<a href="competitive-programming.html">CP Roadmap</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">Languages & Systems</div>
<a href="cpp.html">C++</a>
<a href="golang.html">Go</a>
<a href="javascript.html">JavaScript Deep Dive</a>
<a href="typescript.html">TypeScript</a>
<a href="nodejs.html">Node.js Internals</a>
<a href="os.html">Operating Systems</a>
<a href="linux.html">Linux</a>
<a href="git.html">Git</a>
<a href="backend.html">Backend</a>
<a href="system-design.html">System Design</a>
<a href="networking.html">Networking</a>
<a href="cloud.html">Cloud & Infrastructure</a>
<a href="docker.html">Docker & Compose</a>
<a href="kubernetes.html">Kubernetes</a>
<a href="message-queues.html">Queues & Pub/Sub</a>
<a href="selfhosting.html">VPS & Self-Hosting</a>
<a href="databases.html">PostgreSQL & MySQL</a>
<a href="stripe.html">Stripe & Payments</a>
<a href="distributed-systems.html">Distributed Systems</a>
<a href="backend-engineering.html">Backend Engineering</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">JS/TS Ecosystem</div>
<a href="js-tooling.html">Tooling & Bundlers</a>
<a href="js-testing.html">Testing</a>
<a href="ts-projects.html">Building with TS</a>
</div>
<div class="sidebar-group">
<div class="sidebar-group-label">More</div>
<a href="seans-brain.html">Sean's Brain</a>
</div>
</nav>
</aside>
<div class="container">
<div class="page-header">
<div class="breadcrumb"><a href="index.html">Home</a> / TypeScript</div>
<h1>TypeScript -- Become a Type Beast</h1>
<p>Go beyond <code>string</code> and <code>number</code>. This page covers everything from generics and conditional types to the compiler internals and tsconfig options that actually matter. After this, you'll never be confused by a TypeScript error again.</p>
</div>
<div class="toc">
<h4>Table of Contents</h4>
<a href="#type-system">1. The Type System -- How TS Thinks</a>
<a href="#generics">2. Generics</a>
<a href="#conditional-types">3. Conditional Types</a>
<a href="#mapped-types">4. Mapped Types</a>
<a href="#utility-types">5. Utility Types</a>
<a href="#keyof-typeof">6. keyof, typeof & Index Access</a>
<a href="#infer">7. The <code>infer</code> Keyword</a>
<a href="#decorators">8. Decorators</a>
<a href="#modules-namespaces">9. Modules, Namespaces & Declaration Files</a>
<a href="#tsconfig">10. tsconfig.json Deep Dive</a>
<a href="#compiler">11. The TypeScript Compiler</a>
<a href="#real-patterns">12. Real-World Patterns & Gotchas</a>
</div>
<!-- ───────────── SECTION 1 ───────────── -->
<section id="type-system">
<h2>1. The Type System -- How TS Thinks</h2>
<p>TypeScript uses a <strong>structural type system</strong> (duck typing), not a nominal one. Two types are compatible if their shapes match, regardless of what they're named.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">interface</span> <span class="function">Dog</span> { name: <span class="keyword">string</span>; bark(): <span class="keyword">void</span> }
<span class="keyword">interface</span> <span class="function">Pet</span> { name: <span class="keyword">string</span> }
<span class="keyword">const</span> d: <span class="function">Dog</span> = { name: <span class="string">"Rex"</span>, bark() { console.log(<span class="string">"woof"</span>) } };
<span class="keyword">const</span> p: <span class="function">Pet</span> = d; <span class="comment">// ✅ OK -- Dog has everything Pet needs</span>
</code></pre>
<div class="tip-box">
<strong>Structural vs Nominal:</strong> In C++ or Java, <code>Dog</code> and <code>Pet</code> would need an explicit <code>extends</code>/<code>implements</code> relationship. In TS, shape is all that matters.
</div>
<h3>Type Narrowing</h3>
<p>TS narrows types through control flow analysis. After a check, the compiler <em>knows</em> the type is more specific.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">function</span> <span class="function">process</span>(val: <span class="keyword">string</span> | <span class="keyword">number</span>) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> val === <span class="string">"string"</span>) {
<span class="comment">// TS knows val is string here</span>
console.log(val.toUpperCase());
} <span class="keyword">else</span> {
<span class="comment">// TS knows val is number here</span>
console.log(val.toFixed(<span class="number">2</span>));
}
}
</code></pre>
<h3>Discriminated Unions</h3>
<p>The most powerful pattern for modeling domain types. Add a literal "tag" field that TS uses to narrow.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">type</span> <span class="function">Shape</span> =
| { kind: <span class="string">"circle"</span>; radius: <span class="keyword">number</span> }
| { kind: <span class="string">"rect"</span>; w: <span class="keyword">number</span>; h: <span class="keyword">number</span> };
<span class="keyword">function</span> <span class="function">area</span>(s: <span class="function">Shape</span>): <span class="keyword">number</span> {
<span class="keyword">switch</span> (s.kind) {
<span class="keyword">case</span> <span class="string">"circle"</span>: <span class="keyword">return</span> Math.PI * s.radius ** <span class="number">2</span>;
<span class="keyword">case</span> <span class="string">"rect"</span>: <span class="keyword">return</span> s.w * s.h;
}
}
</code></pre>
</section>
<!-- ───────────── SECTION 2 ───────────── -->
<section id="generics">
<h2>2. Generics</h2>
<p>Generics let you write code that works with <em>any</em> type while keeping type safety. Think of <code><T></code> as a <strong>type parameter</strong> -- a variable, but for types.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// Without generics -- loses type info</span>
<span class="keyword">function</span> <span class="function">identity</span>(val: <span class="keyword">any</span>): <span class="keyword">any</span> { <span class="keyword">return</span> val; }
<span class="comment">// With generics -- T flows through</span>
<span class="keyword">function</span> <span class="function">identity</span><T>(val: T): T { <span class="keyword">return</span> val; }
<span class="keyword">const</span> num = <span class="function">identity</span>(<span class="number">42</span>); <span class="comment">// type: number (inferred)</span>
<span class="keyword">const</span> str = <span class="function">identity</span>(<span class="string">"hi"</span>); <span class="comment">// type: string (inferred)</span>
</code></pre>
<h3>Generic Constraints</h3>
<p>Use <code>extends</code> to constrain what <code>T</code> can be.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">function</span> <span class="function">getLength</span><T <span class="keyword">extends</span> { length: <span class="keyword">number</span> }>(item: T): <span class="keyword">number</span> {
<span class="keyword">return</span> item.length;
}
<span class="function">getLength</span>(<span class="string">"hello"</span>); <span class="comment">// ✅ string has .length</span>
<span class="function">getLength</span>([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]); <span class="comment">// ✅ array has .length</span>
<span class="function">getLength</span>(<span class="number">42</span>); <span class="comment">// ❌ number has no .length</span>
</code></pre>
<h3>Multiple Type Parameters</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">function</span> <span class="function">merge</span><A, B>(a: A, b: B): A & B {
<span class="keyword">return</span> { ...a, ...b };
}
<span class="keyword">const</span> result = <span class="function">merge</span>({ name: <span class="string">"Sean"</span> }, { age: <span class="number">25</span> });
<span class="comment">// type: { name: string } & { age: number }</span>
</code></pre>
<h3>Generic Interfaces & Classes</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">interface</span> <span class="function">Repository</span><T> {
getById(id: <span class="keyword">string</span>): Promise<T | <span class="keyword">null</span>>;
save(entity: T): Promise<<span class="keyword">void</span>>;
}
<span class="keyword">class</span> <span class="function">UserRepo</span> <span class="keyword">implements</span> <span class="function">Repository</span><User> {
<span class="keyword">async</span> <span class="function">getById</span>(id: <span class="keyword">string</span>) { <span class="comment">/* ... */</span> }
<span class="keyword">async</span> <span class="function">save</span>(user: User) { <span class="comment">/* ... */</span> }
}
</code></pre>
<h3>Default Type Parameters</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">interface</span> <span class="function">ApiResponse</span><T = <span class="keyword">unknown</span>> {
data: T;
status: <span class="keyword">number</span>;
}
<span class="keyword">const</span> res: <span class="function">ApiResponse</span> = { data: <span class="string">"anything"</span>, status: <span class="number">200</span> };
<span class="keyword">const</span> typed: <span class="function">ApiResponse</span><User> = { data: user, status: <span class="number">200</span> };
</code></pre>
</section>
<!-- ───────────── SECTION 3 ───────────── -->
<section id="conditional-types">
<h2>3. Conditional Types</h2>
<p>Conditional types are <strong>if/else for types</strong>. The syntax mirrors the ternary operator.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// Syntax: T extends U ? TrueType : FalseType</span>
<span class="keyword">type</span> <span class="function">IsString</span><T> = T <span class="keyword">extends</span> <span class="keyword">string</span> ? <span class="string">"yes"</span> : <span class="string">"no"</span>;
<span class="keyword">type</span> A = <span class="function">IsString</span><<span class="string">"hello"</span>>; <span class="comment">// "yes"</span>
<span class="keyword">type</span> B = <span class="function">IsString</span><<span class="number">42</span>>; <span class="comment">// "no"</span>
</code></pre>
<h3>Distributive Conditional Types</h3>
<p>When <code>T</code> is a <strong>union</strong>, the conditional distributes over each member individually.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">type</span> <span class="function">ToArray</span><T> = T <span class="keyword">extends</span> <span class="keyword">any</span> ? T[] : <span class="keyword">never</span>;
<span class="keyword">type</span> R = <span class="function">ToArray</span><<span class="keyword">string</span> | <span class="keyword">number</span>>;
<span class="comment">// = string[] | number[] (NOT (string | number)[])</span>
<span class="comment">// To prevent distribution, wrap in tuple:</span>
<span class="keyword">type</span> <span class="function">ToArrayND</span><T> = [T] <span class="keyword">extends</span> [<span class="keyword">any</span>] ? T[] : <span class="keyword">never</span>;
<span class="keyword">type</span> R2 = <span class="function">ToArrayND</span><<span class="keyword">string</span> | <span class="keyword">number</span>>;
<span class="comment">// = (string | number)[]</span>
</code></pre>
<h3>Nested Conditional Types</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">type</span> <span class="function">TypeName</span><T> =
T <span class="keyword">extends</span> <span class="keyword">string</span> ? <span class="string">"string"</span> :
T <span class="keyword">extends</span> <span class="keyword">number</span> ? <span class="string">"number"</span> :
T <span class="keyword">extends</span> <span class="keyword">boolean</span> ? <span class="string">"boolean"</span> :
T <span class="keyword">extends</span> Function ? <span class="string">"function"</span> :
<span class="string">"object"</span>;
<span class="keyword">type</span> T1 = <span class="function">TypeName</span><<span class="keyword">string</span>>; <span class="comment">// "string"</span>
<span class="keyword">type</span> T2 = <span class="function">TypeName</span><() => <span class="keyword">void</span>>; <span class="comment">// "function"</span>
</code></pre>
</section>
<!-- ───────────── SECTION 4 ───────────── -->
<section id="mapped-types">
<h2>4. Mapped Types</h2>
<p>Mapped types transform every property of an existing type. They iterate over keys with <code>in</code>.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// Make every property optional</span>
<span class="keyword">type</span> <span class="function">MyPartial</span><T> = {
[K <span class="keyword">in</span> <span class="keyword">keyof</span> T]?: T[K];
};
<span class="comment">// Make every property readonly</span>
<span class="keyword">type</span> <span class="function">MyReadonly</span><T> = {
<span class="keyword">readonly</span> [K <span class="keyword">in</span> <span class="keyword">keyof</span> T]: T[K];
};
</code></pre>
<h3>Key Remapping with <code>as</code></h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">type</span> <span class="function">Getters</span><T> = {
[K <span class="keyword">in</span> <span class="keyword">keyof</span> T <span class="keyword">as</span> `get${Capitalize<<span class="keyword">string</span> & K>}`]: () => T[K];
};
<span class="keyword">interface</span> <span class="function">User</span> { name: <span class="keyword">string</span>; age: <span class="keyword">number</span> }
<span class="keyword">type</span> UserGetters = <span class="function">Getters</span><User>;
<span class="comment">// { getName: () => string; getAge: () => number }</span>
</code></pre>
<h3>Filtering Keys</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// Keep only string properties</span>
<span class="keyword">type</span> <span class="function">StringProps</span><T> = {
[K <span class="keyword">in</span> <span class="keyword">keyof</span> T <span class="keyword">as</span> T[K] <span class="keyword">extends</span> <span class="keyword">string</span> ? K : <span class="keyword">never</span>]: T[K];
};
<span class="keyword">type</span> R = <span class="function">StringProps</span><{ name: <span class="keyword">string</span>; age: <span class="keyword">number</span>; email: <span class="keyword">string</span> }>;
<span class="comment">// { name: string; email: string }</span>
</code></pre>
</section>
<!-- ───────────── SECTION 5 ───────────── -->
<section id="utility-types">
<h2>5. Utility Types</h2>
<p>TS ships with utility types built from generics + conditionals + mapped types. Here's how they work under the hood.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// Partial<T> -- make all props optional</span>
<span class="keyword">type</span> Partial<T> = { [K <span class="keyword">in</span> <span class="keyword">keyof</span> T]?: T[K] };
<span class="comment">// Required<T> -- make all props required</span>
<span class="keyword">type</span> Required<T> = { [K <span class="keyword">in</span> <span class="keyword">keyof</span> T]-?: T[K] };
<span class="comment">// Readonly<T> -- make all props readonly</span>
<span class="keyword">type</span> Readonly<T> = { <span class="keyword">readonly</span> [K <span class="keyword">in</span> <span class="keyword">keyof</span> T]: T[K] };
<span class="comment">// Pick<T, K> -- select specific keys</span>
<span class="keyword">type</span> Pick<T, K <span class="keyword">extends</span> <span class="keyword">keyof</span> T> = { [P <span class="keyword">in</span> K]: T[P] };
<span class="comment">// Omit<T, K> -- remove specific keys</span>
<span class="keyword">type</span> Omit<T, K <span class="keyword">extends</span> <span class="keyword">keyof</span> <span class="keyword">any</span>> = Pick<T, Exclude<<span class="keyword">keyof</span> T, K>>;
<span class="comment">// Record<K, V> -- build an object type</span>
<span class="keyword">type</span> Record<K <span class="keyword">extends</span> <span class="keyword">keyof</span> <span class="keyword">any</span>, V> = { [P <span class="keyword">in</span> K]: V };
<span class="comment">// Exclude<T, U> -- remove types from a union</span>
<span class="keyword">type</span> Exclude<T, U> = T <span class="keyword">extends</span> U ? <span class="keyword">never</span> : T;
<span class="comment">// Extract<T, U> -- keep types from a union</span>
<span class="keyword">type</span> Extract<T, U> = T <span class="keyword">extends</span> U ? T : <span class="keyword">never</span>;
<span class="comment">// NonNullable<T> -- remove null and undefined</span>
<span class="keyword">type</span> NonNullable<T> = T <span class="keyword">extends</span> <span class="keyword">null</span> | <span class="keyword">undefined</span> ? <span class="keyword">never</span> : T;
<span class="comment">// ReturnType<T> -- extract function return type</span>
<span class="keyword">type</span> ReturnType<T <span class="keyword">extends</span> (...args: <span class="keyword">any</span>) => <span class="keyword">any</span>> =
T <span class="keyword">extends</span> (...args: <span class="keyword">any</span>) => <span class="keyword">infer</span> R ? R : <span class="keyword">any</span>;
<span class="comment">// Parameters<T> -- extract function params as tuple</span>
<span class="keyword">type</span> Parameters<T <span class="keyword">extends</span> (...args: <span class="keyword">any</span>) => <span class="keyword">any</span>> =
T <span class="keyword">extends</span> (...args: <span class="keyword">infer</span> P) => <span class="keyword">any</span> ? P : <span class="keyword">never</span>;
</code></pre>
<div class="example-box">
<strong>Using them together:</strong>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">interface</span> <span class="function">User</span> { id: <span class="keyword">string</span>; name: <span class="keyword">string</span>; email: <span class="keyword">string</span>; role: <span class="keyword">string</span> }
<span class="keyword">type</span> CreateUserInput = Omit<User, <span class="string">"id"</span>>;
<span class="keyword">type</span> UpdateUserInput = Partial<Pick<User, <span class="string">"name"</span> | <span class="string">"email"</span>>>;
<span class="keyword">type</span> UserLookup = Record<<span class="keyword">string</span>, User>;
</code></pre>
</div>
</section>
<!-- ───────────── SECTION 6 ───────────── -->
<section id="keyof-typeof">
<h2>6. keyof, typeof & Index Access</h2>
<h3>keyof -- Get All Keys</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">interface</span> <span class="function">Config</span> { host: <span class="keyword">string</span>; port: <span class="keyword">number</span>; debug: <span class="keyword">boolean</span> }
<span class="keyword">type</span> ConfigKey = <span class="keyword">keyof</span> Config; <span class="comment">// "host" | "port" | "debug"</span>
<span class="keyword">function</span> <span class="function">getConfig</span><K <span class="keyword">extends</span> <span class="keyword">keyof</span> Config>(key: K): Config[K] {
<span class="keyword">return</span> config[key];
}
<span class="keyword">const</span> h = <span class="function">getConfig</span>(<span class="string">"host"</span>); <span class="comment">// type: string</span>
<span class="keyword">const</span> p = <span class="function">getConfig</span>(<span class="string">"port"</span>); <span class="comment">// type: number</span>
</code></pre>
<h3>typeof -- Get Type from Value</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">const</span> defaults = { host: <span class="string">"localhost"</span>, port: <span class="number">3000</span>, debug: <span class="keyword">false</span> };
<span class="keyword">type</span> Config = <span class="keyword">typeof</span> defaults;
<span class="comment">// { host: string; port: number; debug: boolean }</span>
<span class="comment">// Combine with keyof:</span>
<span class="keyword">type</span> ConfigKey = <span class="keyword">keyof</span> <span class="keyword">typeof</span> defaults;
<span class="comment">// "host" | "port" | "debug"</span>
</code></pre>
<h3>Index Access Types</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">interface</span> <span class="function">User</span> { name: <span class="keyword">string</span>; address: { city: <span class="keyword">string</span>; zip: <span class="keyword">string</span> } }
<span class="keyword">type</span> Address = User[<span class="string">"address"</span>]; <span class="comment">// { city: string; zip: string }</span>
<span class="keyword">type</span> City = User[<span class="string">"address"</span>][<span class="string">"city"</span>]; <span class="comment">// string</span>
<span class="comment">// Index into arrays:</span>
<span class="keyword">const</span> roles = [<span class="string">"admin"</span>, <span class="string">"user"</span>, <span class="string">"guest"</span>] <span class="keyword">as const</span>;
<span class="keyword">type</span> Role = (<span class="keyword">typeof</span> roles)[<span class="keyword">number</span>]; <span class="comment">// "admin" | "user" | "guest"</span>
</code></pre>
</section>
<!-- ───────────── SECTION 7 ───────────── -->
<section id="infer">
<h2>7. The <code>infer</code> Keyword</h2>
<p><code>infer</code> lets you <strong>extract types from within other types</strong> inside conditional type branches. Think of it as pattern matching for types.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// Extract the return type of a function</span>
<span class="keyword">type</span> <span class="function">MyReturnType</span><T> =
T <span class="keyword">extends</span> (...args: <span class="keyword">any</span>[]) => <span class="keyword">infer</span> R ? R : <span class="keyword">never</span>;
<span class="keyword">type</span> R1 = <span class="function">MyReturnType</span><() => <span class="keyword">string</span>>; <span class="comment">// string</span>
<span class="keyword">type</span> R2 = <span class="function">MyReturnType</span><(x: <span class="keyword">number</span>) => User>; <span class="comment">// User</span>
</code></pre>
<h3>More infer Patterns</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// Extract element type from array</span>
<span class="keyword">type</span> <span class="function">ElementOf</span><T> = T <span class="keyword">extends</span> (infer E)[] ? E : <span class="keyword">never</span>;
<span class="keyword">type</span> E = <span class="function">ElementOf</span><<span class="keyword">string</span>[]>; <span class="comment">// string</span>
<span class="comment">// Extract Promise inner type</span>
<span class="keyword">type</span> <span class="function">Awaited</span><T> = T <span class="keyword">extends</span> Promise<<span class="keyword">infer</span> U> ? U : T;
<span class="keyword">type</span> A = <span class="function">Awaited</span><Promise<<span class="keyword">string</span>>>; <span class="comment">// string</span>
<span class="comment">// Extract first arg of a function</span>
<span class="keyword">type</span> <span class="function">FirstArg</span><T> =
T <span class="keyword">extends</span> (first: <span class="keyword">infer</span> F, ...rest: <span class="keyword">any</span>[]) => <span class="keyword">any</span> ? F : <span class="keyword">never</span>;
<span class="keyword">type</span> F = <span class="function">FirstArg</span><(name: <span class="keyword">string</span>, age: <span class="keyword">number</span>) => <span class="keyword">void</span>>; <span class="comment">// string</span>
<span class="comment">// Extract the type from a class constructor</span>
<span class="keyword">type</span> <span class="function">InstanceOf</span><T> =
T <span class="keyword">extends</span> <span class="keyword">new</span> (...args: <span class="keyword">any</span>[]) => <span class="keyword">infer</span> I ? I : <span class="keyword">never</span>;
</code></pre>
<div class="tip-box">
<strong>Rule:</strong> <code>infer</code> can only be used inside the <code>extends</code> clause of a conditional type. Think of it as "capture whatever type appears in this position."
</div>
</section>
<!-- ───────────── SECTION 8 ───────────── -->
<section id="decorators">
<h2>8. Decorators</h2>
<p>Decorators are <strong>functions that modify classes, methods, or properties</strong> at definition time. They're used heavily in frameworks like NestJS, TypeORM, and Angular.</p>
<div class="warning-box">
<strong>Stage 3 Decorators (TC39):</strong> TS 5.0+ supports the new standard decorator syntax. Older code uses <code>"experimentalDecorators": true</code> in tsconfig. The new standard does NOT require that flag.
</div>
<h3>Class Decorators</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// A decorator is just a function that receives the target</span>
<span class="keyword">function</span> <span class="function">Sealed</span>(target: Function) {
Object.seal(target);
Object.seal(target.prototype);
}
@<span class="function">Sealed</span>
<span class="keyword">class</span> <span class="function">UserService</span> {
<span class="function">getUser</span>() { <span class="comment">/* ... */</span> }
}
</code></pre>
<h3>Method Decorators</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">function</span> <span class="function">Log</span>(target: <span class="keyword">any</span>, key: <span class="keyword">string</span>, descriptor: PropertyDescriptor) {
<span class="keyword">const</span> original = descriptor.value;
descriptor.value = <span class="keyword">function</span>(...args: <span class="keyword">any</span>[]) {
console.log(`Calling ${key} with`, args);
<span class="keyword">return</span> original.apply(<span class="keyword">this</span>, args);
};
}
<span class="keyword">class</span> <span class="function">MathService</span> {
@<span class="function">Log</span>
<span class="function">add</span>(a: <span class="keyword">number</span>, b: <span class="keyword">number</span>) { <span class="keyword">return</span> a + b; }
}
</code></pre>
<h3>Decorator Factories (Decorators with Args)</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">function</span> <span class="function">MinLength</span>(min: <span class="keyword">number</span>) {
<span class="keyword">return function</span>(target: <span class="keyword">any</span>, key: <span class="keyword">string</span>) {
<span class="keyword">let</span> value: <span class="keyword">string</span>;
Object.defineProperty(target, key, {
get: () => value,
set: (newVal: <span class="keyword">string</span>) => {
<span class="keyword">if</span> (newVal.length < min)
<span class="keyword">throw new</span> Error(`${key} must be at least ${min} chars`);
value = newVal;
}
});
};
}
<span class="keyword">class</span> <span class="function">User</span> {
@<span class="function">MinLength</span>(<span class="number">3</span>)
name: <span class="keyword">string</span> = <span class="string">""</span>;
}
</code></pre>
</section>
<!-- ───────────── SECTION 9 ───────────── -->
<section id="modules-namespaces">
<h2>9. Modules, Namespaces & Declaration Files</h2>
<h3>ES Modules in TypeScript</h3>
<p>TS follows the ESM standard. Any file with a top-level <code>import</code> or <code>export</code> is a module.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// math.ts</span>
<span class="keyword">export function</span> <span class="function">add</span>(a: <span class="keyword">number</span>, b: <span class="keyword">number</span>): <span class="keyword">number</span> {
<span class="keyword">return</span> a + b;
}
<span class="comment">// app.ts</span>
<span class="keyword">import</span> { add } <span class="keyword">from</span> <span class="string">"./math"</span>; <span class="comment">// named import</span>
<span class="keyword">import</span> * <span class="keyword">as</span> math <span class="keyword">from</span> <span class="string">"./math"</span>; <span class="comment">// namespace import</span>
</code></pre>
<h3>Namespaces (Legacy)</h3>
<p>Namespaces predate ES modules. They're still used in <code>.d.ts</code> files and ambient declarations but should be avoided in application code.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">namespace</span> Validation {
<span class="keyword">export interface</span> <span class="function">Validator</span> {
isValid(s: <span class="keyword">string</span>): <span class="keyword">boolean</span>;
}
<span class="keyword">export class</span> <span class="function">EmailValidator</span> <span class="keyword">implements</span> <span class="function">Validator</span> {
<span class="function">isValid</span>(s: <span class="keyword">string</span>) { <span class="keyword">return</span> s.includes(<span class="string">"@"</span>); }
}
}
<span class="keyword">const</span> v = <span class="keyword">new</span> Validation.<span class="function">EmailValidator</span>();
</code></pre>
<h3>Declaration Files (<code>.d.ts</code>)</h3>
<p>Declaration files describe the shape of JS code so TS can type-check it. They contain only types, no runtime code.</p>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// global.d.ts -- add types for things TS doesn't know about</span>
<span class="keyword">declare module</span> <span class="string">"*.css"</span> {
<span class="keyword">const</span> classes: { [key: <span class="keyword">string</span>]: <span class="keyword">string</span> };
<span class="keyword">export default</span> classes;
}
<span class="keyword">declare module</span> <span class="string">"*.svg"</span> {
<span class="keyword">const</span> content: <span class="keyword">string</span>;
<span class="keyword">export default</span> content;
}
</code></pre>
<div class="warning-box">
<strong>This is why CSS imports cause TS errors!</strong> TypeScript doesn't know what a <code>.css</code> file exports. You need a declaration file (like above) or the <code>*.css</code> module declaration in a <code>.d.ts</code> file that your tsconfig includes. Without it, TS says "Cannot find module './styles.css'." Same applies to <code>.svg</code>, <code>.png</code>, <code>.json</code> (unless <code>resolveJsonModule</code> is on), etc.
</div>
<h3>declare module for Augmenting Existing Types</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// Extend Express Request type to add your custom user field</span>
<span class="keyword">declare module</span> <span class="string">"express"</span> {
<span class="keyword">interface</span> <span class="function">Request</span> {
user?: { id: <span class="keyword">string</span>; role: <span class="keyword">string</span> };
}
}
<span class="comment">// Now req.user is typed everywhere in your Express app</span>
</code></pre>
<h3>Triple-Slash Directives</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">/// <reference types="node" /></span>
<span class="comment">/// <reference path="./globals.d.ts" /></span>
<span class="comment">// These are legacy -- prefer tsconfig "types" and "include" instead</span>
</code></pre>
</section>
<!-- ───────────── SECTION 10 ───────────── -->
<section id="tsconfig">
<h2>10. tsconfig.json Deep Dive</h2>
<p>Every TS project has a <code>tsconfig.json</code>. Here are the options that actually matter, grouped by what they do.</p>
<h3>Module & Resolution</h3>
<pre><code><span class="lang-label">JSON</span>
{
<span class="string">"compilerOptions"</span>: {
<span class="comment">// What module system to output</span>
<span class="string">"module"</span>: <span class="string">"NodeNext"</span>, <span class="comment">// or "ESNext", "CommonJS"</span>
<span class="comment">// How TS finds modules when you import</span>
<span class="string">"moduleResolution"</span>: <span class="string">"NodeNext"</span>, <span class="comment">// or "Bundler" for Vite/Webpack</span>
<span class="comment">// Where compiled JS goes</span>
<span class="string">"outDir"</span>: <span class="string">"./dist"</span>,
<span class="comment">// Root of source files</span>
<span class="string">"rootDir"</span>: <span class="string">"./src"</span>,
<span class="comment">// Allow importing JSON files</span>
<span class="string">"resolveJsonModule"</span>: <span class="keyword">true</span>,
<span class="comment">// Path aliases: import from "@/utils" instead of "../../utils"</span>
<span class="string">"baseUrl"</span>: <span class="string">"."</span>,
<span class="string">"paths"</span>: {
<span class="string">"@/*"</span>: [<span class="string">"src/*"</span>]
}
}
}
</code></pre>
<h3>Strictness</h3>
<pre><code><span class="lang-label">JSON</span>
{
<span class="string">"compilerOptions"</span>: {
<span class="string">"strict"</span>: <span class="keyword">true</span>, <span class="comment">// Enables ALL strict checks below:</span>
<span class="comment">// "strictNullChecks": true -- null/undefined are their own types</span>
<span class="comment">// "strictFunctionTypes": true -- stricter function type checking</span>
<span class="comment">// "strictBindCallApply": true -- check bind/call/apply args</span>
<span class="comment">// "noImplicitAny": true -- error on implicit 'any'</span>
<span class="comment">// "noImplicitThis": true -- error on implicit 'this'</span>
<span class="string">"noUncheckedIndexedAccess"</span>: <span class="keyword">true</span>, <span class="comment">// obj[key] includes undefined</span>
<span class="string">"noUnusedLocals"</span>: <span class="keyword">true</span>,
<span class="string">"noUnusedParameters"</span>: <span class="keyword">true</span>
}
}
</code></pre>
<h3>Target & Lib</h3>
<pre><code><span class="lang-label">JSON</span>
{
<span class="string">"compilerOptions"</span>: {
<span class="comment">// What JS version to compile down to</span>
<span class="string">"target"</span>: <span class="string">"ES2022"</span>,
<span class="comment">// What APIs are available (DOM, ES2022, etc.)</span>
<span class="string">"lib"</span>: [<span class="string">"ES2022"</span>, <span class="string">"DOM"</span>, <span class="string">"DOM.Iterable"</span>],
<span class="comment">// Emit .d.ts files for libraries</span>
<span class="string">"declaration"</span>: <span class="keyword">true</span>,
<span class="comment">// Generate source maps for debugging</span>
<span class="string">"sourceMap"</span>: <span class="keyword">true</span>,
<span class="comment">// Allow JS files in TS project</span>
<span class="string">"allowJs"</span>: <span class="keyword">true</span>,
<span class="comment">// Skip type-checking node_modules .d.ts</span>
<span class="string">"skipLibCheck"</span>: <span class="keyword">true</span>
}
}
</code></pre>
<h3>Interop Flags</h3>
<pre><code><span class="lang-label">JSON</span>
{
<span class="string">"compilerOptions"</span>: {
<span class="comment">// Allow `import React from "react"` even if module has no default export</span>
<span class="string">"esModuleInterop"</span>: <span class="keyword">true</span>,
<span class="comment">// Allow `import x from "./file"` where file is CJS</span>
<span class="string">"allowSyntheticDefaultImports"</span>: <span class="keyword">true</span>,
<span class="comment">// Ensure consistent casing in imports (catches bugs on case-insensitive OS)</span>
<span class="string">"forceConsistentCasingInFileNames"</span>: <span class="keyword">true</span>,
<span class="comment">// Each file is treated as a separate module (faster builds, needed for Babel)</span>
<span class="string">"isolatedModules"</span>: <span class="keyword">true</span>
}
}
</code></pre>
<div class="tip-box">
<strong>Common tsconfig starters:</strong><br>
<code>npx tsc --init</code> -- generates a tsconfig with all options commented out<br>
<code>@tsconfig/node20</code> -- community base config for Node 20<br>
<code>@tsconfig/strictest</code> -- maximum strictness
</div>
</section>
<!-- ───────────── SECTION 11 ───────────── -->
<section id="compiler">
<h2>11. The TypeScript Compiler</h2>
<p>The TS compiler (<code>tsc</code>) does two separate jobs: <strong>type checking</strong> and <strong>code emission</strong>. Modern setups often split these.</p>
<pre><code><span class="lang-label">Bash</span>
<span class="comment"># Type-check only (no output)</span>
tsc --noEmit
<span class="comment"># Compile and emit JS</span>
tsc
<span class="comment"># Watch mode</span>
tsc --watch
<span class="comment"># Check a specific file</span>
tsc --noEmit src/index.ts
</code></pre>
<h3>The Compilation Pipeline</h3>
<pre><code><span class="lang-label">Text</span>
Source (.ts) → Scanner → Tokens → Parser → AST → Binder → Symbols
→ Type Checker → Diagnostics (errors)
→ Emitter → Output (.js, .d.ts, .js.map)
</code></pre>
<h3>Why Modern Projects Don't Use tsc for Bundling</h3>
<div class="example-box">
<strong>Common modern setup:</strong>
<ul>
<li><strong>tsc --noEmit</strong> in CI for type checking only</li>
<li><strong>esbuild / swc / Vite</strong> for fast compilation (strips types, no type checking)</li>
<li><strong>tsup</strong> for library bundling (uses esbuild under the hood)</li>
</ul>
<p>This gives you fast builds + full type safety from separate type-check step.</p>
</div>
<h3>Project References</h3>
<p>For monorepos, use <code>references</code> to compile packages in order.</p>
<pre><code><span class="lang-label">JSON</span>
<span class="comment">// tsconfig.json (root)</span>
{
<span class="string">"references"</span>: [
{ <span class="string">"path"</span>: <span class="string">"./packages/shared"</span> },
{ <span class="string">"path"</span>: <span class="string">"./packages/api"</span> },
{ <span class="string">"path"</span>: <span class="string">"./packages/web"</span> }
]
}
</code></pre>
</section>
<!-- ───────────── SECTION 12 ───────────── -->
<section id="real-patterns">
<h2>12. Real-World Patterns & Gotchas</h2>
<h3>Type Assertion vs Type Guard</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// Assertion (you're telling TS, "trust me") -- risky</span>
<span class="keyword">const</span> el = document.getElementById(<span class="string">"app"</span>) <span class="keyword">as</span> HTMLDivElement;
<span class="comment">// Type guard (TS verifies at runtime) -- safe</span>
<span class="keyword">function</span> <span class="function">isHTMLDiv</span>(el: Element | <span class="keyword">null</span>): el <span class="keyword">is</span> HTMLDivElement {
<span class="keyword">return</span> el <span class="keyword">instanceof</span> HTMLDivElement;
}
</code></pre>
<h3>Exhaustive Checking with never</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">type</span> <span class="function">Action</span> = { type: <span class="string">"add"</span> } | { type: <span class="string">"remove"</span> } | { type: <span class="string">"update"</span> };
<span class="keyword">function</span> <span class="function">handle</span>(action: Action) {
<span class="keyword">switch</span> (action.type) {
<span class="keyword">case</span> <span class="string">"add"</span>: <span class="keyword">break</span>;
<span class="keyword">case</span> <span class="string">"remove"</span>: <span class="keyword">break</span>;
<span class="keyword">case</span> <span class="string">"update"</span>: <span class="keyword">break</span>;
<span class="keyword">default</span>: {
<span class="comment">// If you miss a case, this line errors at compile time</span>
<span class="keyword">const</span> _exhaustive: <span class="keyword">never</span> = action;
<span class="keyword">throw new</span> Error(`Unhandled: ${_exhaustive}`);
}
}
}
</code></pre>
<h3>Branded / Opaque Types</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="comment">// Prevent mixing up IDs that are all strings</span>
<span class="keyword">type</span> <span class="function">UserId</span> = <span class="keyword">string</span> & { __brand: <span class="string">"UserId"</span> };
<span class="keyword">type</span> <span class="function">PostId</span> = <span class="keyword">string</span> & { __brand: <span class="string">"PostId"</span> };
<span class="keyword">function</span> <span class="function">getUser</span>(id: UserId) { <span class="comment">/* ... */</span> }
<span class="keyword">const</span> uid = <span class="string">"abc"</span> <span class="keyword">as</span> UserId;
<span class="keyword">const</span> pid = <span class="string">"abc"</span> <span class="keyword">as</span> PostId;
<span class="function">getUser</span>(uid); <span class="comment">// ✅</span>
<span class="function">getUser</span>(pid); <span class="comment">// ❌ Type 'PostId' is not assignable to 'UserId'</span>
</code></pre>
<h3>Template Literal Types</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">type</span> <span class="function">HttpMethod</span> = <span class="string">"GET"</span> | <span class="string">"POST"</span> | <span class="string">"PUT"</span> | <span class="string">"DELETE"</span>;
<span class="keyword">type</span> <span class="function">ApiRoute</span> = `/${<span class="keyword">string</span>}`;
<span class="keyword">type</span> <span class="function">Endpoint</span> = `${HttpMethod} ${ApiRoute}`;
<span class="comment">// "GET /users" ✅ "PATCH /users" ❌</span>
<span class="comment">// Powerful with mapped types:</span>
<span class="keyword">type</span> <span class="function">EventMap</span><T> = {
[K <span class="keyword">in</span> <span class="keyword">keyof</span> T <span class="keyword">as</span> `on${Capitalize<<span class="keyword">string</span> & K>}`]: (val: T[K]) => <span class="keyword">void</span>;
};
</code></pre>
<h3>The satisfies Operator (TS 4.9+)</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">type</span> <span class="function">Colors</span> = Record<<span class="keyword">string</span>, [<span class="keyword">number</span>, <span class="keyword">number</span>, <span class="keyword">number</span>] | <span class="keyword">string</span>>;
<span class="comment">// 'satisfies' validates the type but keeps the NARROW inferred type</span>
<span class="keyword">const</span> palette = {
red: [<span class="number">255</span>, <span class="number">0</span>, <span class="number">0</span>],
green: <span class="string">"#00ff00"</span>,
} <span class="keyword">satisfies</span> Colors;
palette.red[<span class="number">0</span>]; <span class="comment">// ✅ type: number (not string | number[] -- kept narrow)</span>
palette.green.toUpperCase(); <span class="comment">// ✅ type: string</span>
</code></pre>
<h3>Common Gotcha: Object.keys Returns string[]</h3>
<pre><code><span class="lang-label">TypeScript</span>
<span class="keyword">const</span> obj = { a: <span class="number">1</span>, b: <span class="number">2</span> };
Object.keys(obj); <span class="comment">// string[], NOT ("a" | "b")[]</span>
<span class="comment">// Why? TS structural typing means obj could have MORE keys at runtime.</span>
<span class="comment">// Workaround when you're sure:</span>
<span class="keyword">const</span> keys = Object.keys(obj) <span class="keyword">as</span> (<span class="keyword">keyof typeof</span> obj)[];
</code></pre>
</section>
</div>
<footer class="site-footer">
<p>© 2025 Better Dev. For personal study use.</p>
<p style="margin-top: 1rem; font-size: 0.85rem;"><a href="https://github.com/pythonwithsean/BetterDev/blob/main/typescript.html" target="_blank" rel="noopener noreferrer" class="edit-link">Edit this page on GitHub <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align: middle; margin-left: 2px;"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path><polyline points="15 3 21 3 21 9"></polyline><line x1="10" y1="14" x2="21" y2="3"></line></svg></a></p>
</footer>
<script>
(function(){
var sidebar=document.querySelector('.sidebar');
var backdrop=document.querySelector('.sidebar-backdrop');
var toggleBtn=document.querySelector('.sidebar-toggle');
var closeBtn=document.querySelector('.sidebar-close');
var si=document.querySelector('.sidebar-search-input');
function openSidebar(){
sidebar.classList.add('open');
backdrop.classList.add('visible');
document.body.classList.add('sidebar-open');
toggleBtn.setAttribute('aria-expanded','true');
if(si)si.focus();
}
function closeSidebar(){
sidebar.classList.remove('open');
backdrop.classList.remove('visible');
document.body.classList.remove('sidebar-open');
toggleBtn.setAttribute('aria-expanded','false');
}
if(toggleBtn)toggleBtn.addEventListener('click',function(){
sidebar.classList.contains('open')?closeSidebar():openSidebar();
});
if(closeBtn)closeBtn.addEventListener('click',closeSidebar);
if(backdrop)backdrop.addEventListener('click',closeSidebar);
document.addEventListener('keydown',function(e){
if(e.key==='Escape'&&sidebar.classList.contains('open'))closeSidebar();
});
var currentPage=window.location.pathname.split('/').pop()||'index.html';
var navLinks=document.querySelectorAll('.sidebar-nav a');
for(var i=0;i<navLinks.length;i++){
if(navLinks[i].getAttribute('href')===currentPage)navLinks[i].classList.add('active');
}
var input=document.querySelector('.sidebar-search-input');
var box=document.querySelector('.sidebar-search-results');
if(!input||!box)return;
var links=[].slice.call(document.querySelectorAll('.sidebar-nav a'));
var topics=links.map(function(a){return{text:a.textContent.trim(),href:a.getAttribute('href')};});
var idx=-1;
function render(q){
if(!q){box.classList.remove('visible');box.innerHTML='';idx=-1;return;}
var lc=q.toLowerCase();
var matches=topics.filter(function(t){return t.text.toLowerCase().indexOf(lc)!==-1;});
if(matches.length===0){box.innerHTML='<div class="no-results">No topics found</div>';box.classList.add('visible');idx=-1;return;}
box.innerHTML=matches.map(function(m){return'<a href="'+m.href+'">'+m.text+'</a>';}).join('');
box.classList.add('visible');
idx=-1;
}
function highlight(items){
for(var i=0;i<items.length;i++){items[i].classList.toggle('highlighted',i===idx);}
}
input.addEventListener('input',function(){render(this.value);});
input.addEventListener('keydown',function(e){
var items=box.querySelectorAll('a');
if(!items.length)return;
if(e.key==='ArrowDown'){e.preventDefault();idx=Math.min(idx+1,items.length-1);highlight(items);items[idx].scrollIntoView({block:'nearest'});}
else if(e.key==='ArrowUp'){e.preventDefault();idx=Math.max(idx-1,0);highlight(items);items[idx].scrollIntoView({block:'nearest'});}
else if(e.key==='Enter'&&idx>=0){e.preventDefault();items[idx].click();}
});
input.addEventListener('blur',function(){setTimeout(function(){box.classList.remove('visible');idx=-1;},200);});
input.addEventListener('focus',function(){if(this.value)render(this.value);});
})();
</script>
</body>
</html>