-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
tsguide.html
4063 lines (2917 loc) · 138 KB
/
tsguide.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>Google TypeScript Style Guide</title>
<link rel="stylesheet" href="javaguide.css">
<script src="include/styleguide.js"></script>
<link rel="shortcut icon" href="https://www.google.com/favicon.ico">
<script src="include/jsguide.js"></script>
</head>
<body onload="initStyleGuide();">
<div id="content">
<h1>Google TypeScript Style Guide</h1>
<section>
<p>This guide is based on the internal Google TypeScript style guide, but it has
been slightly adjusted to remove Google-internal sections. Google's internal
environment has different constraints on TypeScript than you might find outside
of Google. The advice here is specifically useful for people authoring code they
intend to import into Google, but otherwise may not apply in your external
environment.</p>
<p>There is no automatic deployment process for this version as it's pushed
on-demand by volunteers.</p>
</section>
<h2 id="introduction" class="numbered">Introduction</h2>
<h3 id="terminology-notes" class="numbered">Terminology notes</h3>
<p>This Style Guide uses <a href="https://tools.ietf.org/html/rfc2119">RFC 2119</a>
terminology when using the phrases <em>must</em>, <em>must not</em>, <em>should</em>, <em>should not</em>,
and <em>may</em>. The terms <em>prefer</em> and <em>avoid</em> correspond to <em>should</em> and <em>should
not</em>, respectively. Imperative and declarative statements are prescriptive and
correspond to <em>must</em>.</p>
<h3 id="guide-notes" class="numbered">Guide notes</h3>
<p>All examples given are <strong>non-normative</strong> and serve only to illustrate the
normative language of the style guide. That is, while the examples are in Google
Style, they may not illustrate the <em>only</em> stylish way to represent the code.
Optional formatting choices made in examples must not be enforced as rules.</p>
<h2 id="source-file-basics" class="numbered">Source file basics</h2>
<p><a id="file-encoding"></a></p>
<h3 id="file-encoding-utf-8" class="numbered">File encoding: UTF-8</h3>
<p>Source files are encoded in <strong>UTF-8</strong>.</p>
<p><a id="special-characters"></a></p>
<h4 id="whitespace-characters" class="numbered">Whitespace characters</h4>
<p>Aside from the line terminator sequence, the ASCII horizontal space character
(0x20) is the only whitespace character that appears anywhere in a source file.
This implies that all other whitespace characters in string literals are
escaped.</p>
<h4 id="special-escape-sequences" class="numbered">Special escape sequences</h4>
<p>For any character that has a special escape sequence (<code>\'</code>, <code>\"</code>, <code>\\</code>, <code>\b</code>,
<code>\f</code>, <code>\n</code>, <code>\r</code>, <code>\t</code>, <code>\v</code>), that sequence is used rather than the
corresponding numeric escape (e.g <code>\x0a</code>, <code>\u000a</code>, or <code>\u{a}</code>). Legacy octal
escapes are never used.</p>
<h4 id="non-ascii-characters" class="numbered">Non-ASCII characters</h4>
<p>For the remaining non-ASCII characters, use the actual Unicode character (e.g.
<code>∞</code>). For non-printable characters, the equivalent hex or Unicode escapes (e.g.
<code>\u221e</code>) can be used along with an explanatory comment.</p>
<pre><code class="language-ts good">// Perfectly clear, even without a comment.
const units = 'μs';
// Use escapes for non-printable characters.
const output = '\ufeff' + content; // byte order mark
</code></pre>
<pre><code class="language-ts bad">// Hard to read and prone to mistakes, even with the comment.
const units = '\u03bcs'; // Greek letter mu, 's'
// The reader has no idea what this is.
const output = '\ufeff' + content;
</code></pre>
<p><a id="modules"></a>
<a id="source-organization"></a></p>
<h2 id="source-file-structure" class="numbered">Source file structure</h2>
<p>Files consist of the following, <strong>in order</strong>:</p>
<ol>
<li>Copyright information, if present</li>
<li>JSDoc with <code>@fileoverview</code>, if present</li>
<li>Imports, if present</li>
<li>The file’s implementation</li>
</ol>
<p><strong>Exactly one blank line</strong> separates each section that is present.</p>
<h3 id="file-copyright" class="numbered">Copyright information</h3>
<p>If license or copyright information is necessary in a file, add it in a JSDoc at
the top of the file. </p>
<p><a id="file-fileoverview"></a>
<a id="jsdoc-top-file-level-comments"></a></p>
<h3 id="fileoverview" class="numbered"><code>@fileoverview</code> JSDoc</h3>
<p>A file may have a top-level <code>@fileoverview</code> JSDoc. If present, it may provide a
description of the file's content, its uses, or information about its
dependencies. Wrapped lines are not indented.</p>
<p>Example:</p>
<pre><code class="language-ts good">/**
* @fileoverview Description of file. Lorem ipsum dolor sit amet, consectetur
* adipiscing elit, sed do eiusmod tempor incididunt.
*/
</code></pre>
<h3 id="imports" class="numbered">Imports</h3>
<p>There are four variants of import statements in ES6 and TypeScript:</p>
<section>
<table>
<thead>
<tr>
<th>Import type</th>
<th>Example</th>
<th>Use for</th>
</tr>
</thead>
<tbody>
<tr>
<td>module[<sup>module_import]</sup>
</td>
<td><code>import * as foo from
'...';</code></td>
<td>TypeScript imports
</td>
</tr>
<tr>
<td>named[<sup>destructuring_import]</sup>
</td>
<td><code>import {SomeThing}
from '...';</code></td>
<td>TypeScript imports
</td>
</tr>
<tr>
<td>default
</td>
<td><code>import SomeThing
from '...';</code>
</td>
<td>Only for other
external code that
requires them</td>
</tr>
<tr>
<td>side-effect
</td>
<td><code>import '...';</code>
</td>
<td>Only to import
libraries for their
side-effects on load
(such as custom
elements)</td>
</tr>
</tbody>
</table>
<pre><code class="language-ts good">// Good: choose between two options as appropriate (see below).
import * as ng from '@angular/core';
import {Foo} from './foo';
// Only when needed: default imports.
import Button from 'Button';
// Sometimes needed to import libraries for their side effects:
import 'jasmine';
import '@polymer/paper-button';
</code></pre>
</section>
<h4 id="import-paths" class="numbered">Import paths</h4>
<p>TypeScript code <em>must</em> use paths to import other TypeScript code. Paths <em>may</em> be
relative, i.e. starting with <code>.</code> or <code>..</code>,
or rooted at the base directory, e.g.
<code>root/path/to/file</code>.</p>
<p>Code <em>should</em> use relative imports (<code>./foo</code>) rather than absolute imports
<code>path/to/foo</code> when referring to files within the same (logical) project as this
allows to move the project around without introducing changes in these imports.</p>
<p>Consider limiting the number of parent steps (<code>../../../</code>) as those can make
module and path structures hard to understand.</p>
<pre><code class="language-ts good">import {Symbol1} from 'path/from/root';
import {Symbol2} from '../parent/file';
import {Symbol3} from './sibling';
</code></pre>
<p><a id="module-versus-destructuring-import"></a></p>
<h4 id="namespace-versus-named-imports" class="numbered">Namespace versus named imports</h4>
<p>Both namespace and named imports can be used.</p>
<p>Prefer named imports for symbols used frequently in a file or for symbols that
have clear names, for example Jasmine's <code>describe</code> and <code>it</code>. Named imports can
be aliased to clearer names as needed with <code>as</code>.</p>
<p>Prefer namespace imports when using many different symbols from large APIs. A
namespace import, despite using the <code>*</code> character, is not comparable to a
<q>wildcard</q> import as seen in other languages. Instead, namespace imports give a
name to all the exports of a module, and each exported symbol from the module
becomes a property on the module name. Namespace imports can aid readability for
exported symbols that have common names like <code>Model</code> or <code>Controller</code> without the
need to declare aliases.</p>
<pre><code class="language-ts bad">// Bad: overlong import statement of needlessly namespaced names.
import {Item as TableviewItem, Header as TableviewHeader, Row as TableviewRow,
Model as TableviewModel, Renderer as TableviewRenderer} from './tableview';
let item: TableviewItem|undefined;
</code></pre>
<pre><code class="language-ts good">// Better: use the module for namespacing.
import * as tableview from './tableview';
let item: tableview.Item|undefined;
</code></pre>
<pre><code class="language-ts bad">import * as testing from './testing';
// Bad: The module name does not improve readability.
testing.describe('foo', () => {
testing.it('bar', () => {
testing.expect(null).toBeNull();
testing.expect(undefined).toBeUndefined();
});
});
</code></pre>
<pre><code class="language-ts good">// Better: give local names for these common functions.
import {describe, it, expect} from './testing';
describe('foo', () => {
it('bar', () => {
expect(null).toBeNull();
expect(undefined).toBeUndefined();
});
});
</code></pre>
<h5 id="jspb-import-by-path" class="numbered">Special case: Apps JSPB protos</h5>
<p>Apps JSPB protos must use named imports, even when it leads to long import
lines.</p>
<p>This rule exists to aid in build performance and dead code elimination since
often <code>.proto</code> files contain many <code>message</code>s that are not all needed together.
By leveraging destructured imports the build system can create finer grained
dependencies on Apps JSPB messages while preserving the ergonomics of path based
imports.</p>
<pre><code class="language-ts good">// Good: import the exact set of symbols you need from the proto file.
import {Foo, Bar} from './foo.proto';
function copyFooBar(foo: Foo, bar: Bar) {...}
</code></pre>
<h4 id="renaming-imports" class="numbered">Renaming imports</h4>
<p>Code <em>should</em> fix name collisions by using a namespace import or renaming the
exports themselves. Code <em>may</em> rename imports (<code>import {SomeThing as
SomeOtherThing}</code>) if needed.</p>
<p>Three examples where renaming can be helpful:</p>
<ol>
<li>If it's necessary to avoid collisions with other imported symbols.</li>
<li>If the imported symbol name is generated.</li>
<li>If importing symbols whose names are unclear by themselves, renaming can
improve code clarity. For example, when using RxJS the <code>from</code> function might
be more readable when renamed to <code>observableFrom</code>.</li>
</ol>
<h3 id="exports" class="numbered">Exports</h3>
<p>Use named exports in all code:</p>
<pre><code class="language-ts good">// Use named exports:
export class Foo { ... }
</code></pre>
<p>Do not use default exports. This ensures that all imports follow a uniform
pattern.</p>
<pre><code class="language-ts bad">// Do not use default exports:
export default class Foo { ... } // BAD!
</code></pre>
<section class="zippy">
<p>Why?</p>
<p>Default exports provide no canonical name, which makes central maintenance
difficult with relatively little benefit to code owners, including potentially
decreased readability:</p>
<pre><code class="language-ts bad">import Foo from './bar'; // Legal.
import Bar from './bar'; // Also legal.
</code></pre>
<p>Named exports have the benefit of erroring when import statements try to import
something that hasn't been declared. In <code>foo.ts</code>:</p>
<pre><code class="language-ts bad">const foo = 'blah';
export default foo;
</code></pre>
<p>And in <code>bar.ts</code>:</p>
<pre><code class="language-ts bad">import {fizz} from './foo';
</code></pre>
<p>Results in <code>error TS2614: Module '"./foo"' has no exported member 'fizz'.</code> While
<code>bar.ts</code>:</p>
<pre><code class="language-ts bad">import fizz from './foo';
</code></pre>
<p>Results in <code>fizz === foo</code>, which is probably unexpected and difficult to debug.</p>
<p>Additionally, default exports encourage people to put everything into one big
object to namespace it all together:</p>
<pre><code class="language-ts bad">export default class Foo {
static SOME_CONSTANT = ...
static someHelpfulFunction() { ... }
...
}
</code></pre>
<p>With the above pattern, we have file scope, which can be used as a namespace. We
also have a perhaps needless second scope (the class <code>Foo</code>) that can be
ambiguously used as both a type and a value in other files.</p>
<p>Instead, prefer use of file scope for namespacing, as well as named exports:</p>
<pre><code class="language-ts good">export const SOME_CONSTANT = ...
export function someHelpfulFunction()
export class Foo {
// only class stuff here
}
</code></pre>
</section>
<h4 id="export-visibility" class="numbered">Export visibility</h4>
<p>TypeScript does not support restricting the visibility for exported symbols.
Only export symbols that are used outside of the module. Generally minimize the
exported API surface of modules.</p>
<h4 id="mutable-exports" class="numbered">Mutable exports</h4>
<p>Regardless of technical support, mutable exports can create hard to understand
and debug code, in particular with re-exports across multiple modules. One way
to paraphrase this style point is that <code>export let</code> is not allowed.</p>
<section>
<pre><code class="language-ts bad">export let foo = 3;
// In pure ES6, foo is mutable and importers will observe the value change after a second.
// In TS, if foo is re-exported by a second file, importers will not see the value change.
window.setTimeout(() => {
foo = 4;
}, 1000 /* ms */);
</code></pre>
</section>
<p>If one needs to support externally accessible and mutable bindings, they
<em>should</em> instead use explicit getter functions.</p>
<pre><code class="language-ts good">let foo = 3;
window.setTimeout(() => {
foo = 4;
}, 1000 /* ms */);
// Use an explicit getter to access the mutable export.
export function getFoo() { return foo; };
</code></pre>
<p>For the common pattern of conditionally exporting either of two values, first do
the conditional check, then the export. Make sure that all exports are final
after the module's body has executed.</p>
<pre><code class="language-ts good">function pickApi() {
if (useOtherApi()) return OtherApi;
return RegularApi;
}
export const SomeApi = pickApi();
</code></pre>
<p><a id="static-containers"></a></p>
<h4 id="container-classes" class="numbered">Container classes</h4>
<p>Do not create container classes with static methods or properties for the sake
of namespacing.</p>
<pre><code class="language-ts bad">export class Container {
static FOO = 1;
static bar() { return 1; }
}
</code></pre>
<p>Instead, export individual constants and functions:</p>
<pre><code class="language-ts good">export const FOO = 1;
export function bar() { return 1; }
</code></pre>
<h3 id="import-export-type" class="numbered">Import and export type</h3>
<h4 id="import-type" class="numbered">Import type</h4>
<p>You may use <code>import type {...}</code> when you use the imported symbol only as a type.
Use regular imports for values:</p>
<pre><code class="language-ts good">import type {Foo} from './foo';
import {Bar} from './foo';
import {type Foo, Bar} from './foo';
</code></pre>
<section class="zippy">
<p>Why?</p>
<p>The TypeScript compiler automatically handles the distinction and does not
insert runtime loads for type references. So why annotate type imports?</p>
<p>The TypeScript compiler can run in 2 modes:</p>
<ul>
<li>In development mode, we typically want quick iteration loops. The compiler
transpiles to JavaScript without full type information. This is much faster,
but requires <code>import type</code> in certain cases.</li>
<li>In production mode, we want correctness. The compiler type checks everything
and ensures <code>import type</code> is used correctly.</li>
</ul>
<p>Note: If you need to force a runtime load for side effects, use <code>import '...';</code>.
See </p>
</section>
<h4 id="export-type" class="numbered">Export type</h4>
<p>Use <code>export type</code> when re-exporting a type, e.g.:</p>
<pre><code class="language-ts good">export type {AnInterface} from './foo';
</code></pre>
<section class="zippy">
<p>Why?</p>
<p><code>export type</code> is useful to allow type re-exports in file-by-file transpilation.
See
<a href="https://www.typescriptlang.org/tsconfig#exports-of-non-value-identifiers"><code>isolatedModules</code> docs</a>.</p>
<p><code>export type</code> might also seem useful to avoid ever exporting a value symbol for
an API. However it does not give guarantees, either: downstream code might still
import an API through a different path. A better way to split & guarantee type
vs value usages of an API is to actually split the symbols into e.g.
<code>UserService</code> and <code>AjaxUserService</code>. This is less error prone and also better
communicates intent.</p>
</section>
<p><a id="namespaces-vs-modules"></a></p>
<h4 id="use-modules-not-namespaces" class="numbered">Use modules not namespaces</h4>
<p>TypeScript supports two methods to organize code: <em>namespaces</em> and <em>modules</em>,
but namespaces are disallowed. That
is, your code <em>must</em> refer to code in other files using imports and exports of
the form <code>import {foo} from 'bar';</code></p>
<p>Your code <em>must not</em> use the <code>namespace Foo { ... }</code> construct. <code>namespace</code>s
<em>may</em> only be used when required to interface with external, third party code.
To semantically namespace your code, use separate files.</p>
<p>Code <em>must not</em> use <code>require</code> (as in <code>import x = require('...');</code>) for imports.
Use ES6 module syntax.</p>
<pre><code class="language-ts bad">// Bad: do not use namespaces:
namespace Rocket {
function launch() { ... }
}
// Bad: do not use <reference>
/// <reference path="..."/>
// Bad: do not use require()
import x = require('mydep');
</code></pre>
<blockquote>
<p>NB: TypeScript <code>namespace</code>s used to be called internal modules and used to use
the <code>module</code> keyword in the form <code>module Foo { ... }</code>. Don't use that either.
Always use ES6 imports.</p>
</blockquote>
<p><a id="language-rules"></a></p>
<h2 id="language-features" class="numbered">Language features</h2>
<p>This section delineates which features may or may not be used, and any
additional constraints on their use.</p>
<p>Language features which are not discussed in this style guide <em>may</em> be used with
no recommendations of their usage.</p>
<p><a id="features-local-variable-declarations"></a></p>
<h3 id="local-variable-declarations" class="numbered">Local variable declarations</h3>
<p><a id="variables"></a>
<a id="features-use-const-and-let"></a></p>
<h4 id="use-const-and-let" class="numbered">Use const and let</h4>
<p>Always use <code>const</code> or <code>let</code> to declare variables. Use <code>const</code> by default, unless
a variable needs to be reassigned. Never use <code>var</code>.</p>
<pre><code class="language-ts good">const foo = otherValue; // Use if "foo" never changes.
let bar = someValue; // Use if "bar" is ever assigned into later on.
</code></pre>
<p><code>const</code> and <code>let</code> are block scoped, like variables in most other languages.
<code>var</code> in JavaScript is function scoped, which can cause difficult to understand
bugs. Don't use it.</p>
<pre><code class="language-ts bad">var foo = someValue; // Don't use - var scoping is complex and causes bugs.
</code></pre>
<p>Variables <em>must not</em> be used before their declaration.</p>
<p><a id="features-one-variable-per-declaration"></a></p>
<h4 id="one-variable-per-declaration" class="numbered">One variable per declaration</h4>
<p>Every local variable declaration declares only one variable: declarations such
as <code class="badcode">let a = 1, b = 2;</code> are not used.</p>
<p><a id="features-array-literals"></a></p>
<h3 id="array-literals" class="numbered">Array literals</h3>
<p><a id="features-arrays-ctor"></a></p>
<h4 id="array-constructor" class="numbered">Do not use the <code>Array</code> constructor</h4>
<p><em>Do not</em> use the <code>Array()</code> constructor, with or without <code>new</code>. It has confusing
and contradictory usage:</p>
<pre><code class="language-ts bad">const a = new Array(2); // [undefined, undefined]
const b = new Array(2, 3); // [2, 3];
</code></pre>
<p>Instead, always use bracket notation to initialize arrays, or <code>from</code> to
initialize an <code>Array</code> with a certain size:</p>
<pre><code class="language-ts good">const a = [2];
const b = [2, 3];
// Equivalent to Array(2):
const c = [];
c.length = 2;
// [0, 0, 0, 0, 0]
Array.from<number>({length: 5}).fill(0);
</code></pre>
<p><a id="features-arrays-non-numeric-properties"></a></p>
<h4 id="do-not-define-properties-on-arrays" class="numbered">Do not define properties on arrays</h4>
<p>Do not define or use non-numeric properties on an array (other than <code>length</code>).
Use a <code>Map</code> (or <code>Object</code>) instead.</p>
<p><a id="features-arrays-spread-operator"></a></p>
<h4 id="array-spread-syntax" class="numbered">Using spread syntax</h4>
<p>Using spread syntax <code>[...foo];</code> is a convenient shorthand for shallow-copying or
concatenating iterables.</p>
<pre><code class="language-ts good">const foo = [
1,
];
const foo2 = [
...foo,
6,
7,
];
const foo3 = [
5,
...foo,
];
foo2[1] === 6;
foo3[1] === 1;
</code></pre>
<p>When using spread syntax, the value being spread <em>must</em> match what is being
created. When creating an array, only spread iterables. Primitives (including
<code>null</code> and <code>undefined</code>) <em>must not</em> be spread.</p>
<pre><code class="language-ts bad">const foo = [7];
const bar = [5, ...(shouldUseFoo && foo)]; // might be undefined
// Creates {0: 'a', 1: 'b', 2: 'c'} but has no length
const fooStrings = ['a', 'b', 'c'];
const ids = {...fooStrings};
</code></pre>
<pre><code class="language-ts good">const foo = shouldUseFoo ? [7] : [];
const bar = [5, ...foo];
const fooStrings = ['a', 'b', 'c'];
const ids = [...fooStrings, 'd', 'e'];
</code></pre>
<p><a id="features-arrays-destructuring"></a></p>
<h4 id="array-destructuring" class="numbered">Array destructuring</h4>
<p>Array literals may be used on the left-hand side of an assignment to perform
destructuring (such as when unpacking multiple values from a single array or
iterable). A final <q>rest</q> element may be included (with no space between the
<code>...</code> and the variable name). Elements should be omitted if they are unused.</p>
<pre><code class="language-ts good">const [a, b, c, ...rest] = generateResults();
let [, b,, d] = someArray;
</code></pre>
<p>Destructuring may also be used for function parameters. Always specify <code>[]</code> as
the default value if a destructured array parameter is optional, and provide
default values on the left hand side:</p>
<pre><code class="language-ts good">function destructured([a = 4, b = 2] = []) { … }
</code></pre>
<p>Disallowed:</p>
<pre><code class="language-ts bad">function badDestructuring([a, b] = [4, 2]) { … }
</code></pre>
<p>Tip: For (un)packing multiple values into a function’s parameter or return,
prefer object destructuring to array destructuring when possible, as it allows
naming the individual elements and specifying a different type for each.</p>
<p><a id="features-object-literals"></a></p>
<h3 id="object-literals" class="numbered">Object literals</h3>
<p><a id="features-objects-ctor"></a></p>
<h4 id="object-constructor" class="numbered">Do not use the <code>Object</code> constructor</h4>
<p>The <code>Object</code> constructor is disallowed. Use an object literal (<code>{}</code> or <code>{a: 0,
b: 1, c: 2}</code>) instead.</p>
<h4 id="iterating-objects" class="numbered">Iterating objects</h4>
<p>Iterating objects with <code>for (... in ...)</code> is error prone. It will include
enumerable properties from the prototype chain.</p>
<p>Do not use unfiltered <code>for (... in ...)</code> statements:</p>
<pre><code class="language-ts bad">for (const x in someObj) {
// x could come from some parent prototype!
}
</code></pre>
<p>Either filter values explicitly with an <code>if</code> statement, or use <code>for (... of
Object.keys(...))</code>.</p>
<pre><code class="language-ts good">for (const x in someObj) {
if (!someObj.hasOwnProperty(x)) continue;
// now x was definitely defined on someObj
}
for (const x of Object.keys(someObj)) { // note: for _of_!
// now x was definitely defined on someObj
}
for (const [key, value] of Object.entries(someObj)) { // note: for _of_!
// now key was definitely defined on someObj
}
</code></pre>
<p><a id="using-the-spread-operator"></a></p>
<h4 id="object-spread-syntax" class="numbered">Using spread syntax</h4>
<p>Using spread syntax <code>{...bar}</code> is a convenient shorthand for creating a shallow
copy of an object. When using spread syntax in object initialization, later
values replace earlier values at the same key.</p>
<pre><code class="language-ts good">const foo = {
num: 1,
};
const foo2 = {
...foo,
num: 5,
};
const foo3 = {
num: 5,
...foo,
}
foo2.num === 5;
foo3.num === 1;
</code></pre>
<p>When using spread syntax, the value being spread <em>must</em> match what is being
created. That is, when creating an object, only objects may be spread; arrays
and primitives (including <code>null</code> and <code>undefined</code>) <em>must not</em> be spread. Avoid
spreading objects that have prototypes other than the Object prototype (e.g.
class definitions, class instances, functions) as the behavior is unintuitive
(only enumerable non-prototype properties are shallow-copied).</p>
<pre><code class="language-ts bad">const foo = {num: 7};
const bar = {num: 5, ...(shouldUseFoo && foo)}; // might be undefined
// Creates {0: 'a', 1: 'b', 2: 'c'} but has no length
const fooStrings = ['a', 'b', 'c'];
const ids = {...fooStrings};
</code></pre>
<pre><code class="language-ts good">const foo = shouldUseFoo ? {num: 7} : {};
const bar = {num: 5, ...foo};
</code></pre>
<p><a id="features-objects-computed-property-names"></a></p>
<h4 id="computed-property-names" class="numbered">Computed property names</h4>
<p>Computed property names (e.g. <code>{['key' + foo()]: 42}</code>) are allowed, and are
considered dict-style (quoted) keys (i.e., must not be mixed with non-quoted
keys) unless the computed property is a
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol">symbol</a>
(e.g. <code>[Symbol.iterator]</code>).</p>
<p><a id="features-objects-destructuring"></a></p>
<h4 id="object-destructuring" class="numbered">Object destructuring</h4>
<p>Object destructuring patterns may be used on the left-hand side of an assignment
to perform destructuring and unpack multiple values from a single object.</p>
<p>Destructured objects may also be used as function parameters, but should be kept
as simple as possible: a single level of unquoted shorthand properties. Deeper
levels of nesting and computed properties may not be used in parameter
destructuring. Specify any default values in the left-hand-side of the
destructured parameter (<code>{str = 'some default'} = {}</code>, rather than
<code class="badcode">{str} = {str: 'some default'}</code>), and if a
destructured object is itself optional, it must default to <code>{}</code>.</p>
<p>Example:</p>
<pre><code class="language-ts good">interface Options {
/** The number of times to do something. */
num?: number;
/** A string to do stuff to. */
str?: string;
}
function destructured({num, str = 'default'}: Options = {}) {}
</code></pre>
<p>Disallowed:</p>
<pre><code class="language-ts bad">function nestedTooDeeply({x: {num, str}}: {x: Options}) {}
function nontrivialDefault({num, str}: Options = {num: 42, str: 'default'}) {}
</code></pre>
<p><a id="formatting-class-literals"></a>
<a id="features-classes"></a></p>
<h3 id="classes" class="numbered">Classes</h3>
<h4 id="class-declarations" class="numbered">Class declarations</h4>
<p>Class declarations <em>must not</em> be terminated with semicolons:</p>
<pre><code class="language-ts good">class Foo {
}
</code></pre>
<pre><code class="language-ts bad">class Foo {
}; // Unnecessary semicolon
</code></pre>
<p>In contrast, statements that contain class expressions <em>must</em> be terminated with
a semicolon:</p>
<pre><code class="language-ts good">export const Baz = class extends Bar {
method(): number {
return this.x;
}
}; // Semicolon here as this is a statement, not a declaration
</code></pre>
<pre><code class="language-ts bad">exports const Baz = class extends Bar {
method(): number {
return this.x;
}
}
</code></pre>
<p>It is neither encouraged nor discouraged to have blank lines separating class
declaration braces from other class content:</p>
<pre><code class="language-ts good">// No spaces around braces - fine.
class Baz {
method(): number {
return this.x;
}
}
// A single space around both braces - also fine.
class Foo {
method(): number {
return this.x;
}
}
</code></pre>
<h4 id="class-method-declarations" class="numbered">Class method declarations</h4>
<p>Class method declarations <em>must not</em> use a semicolon to separate individual
method declarations:</p>
<pre><code class="language-ts good">class Foo {
doThing() {
console.log("A");
}
}
</code></pre>
<pre><code class="language-ts bad">class Foo {
doThing() {
console.log("A");
}; // <-- unnecessary
}
</code></pre>
<p>Method declarations should be separated from surrounding code by a single blank
line:</p>
<pre><code class="language-ts good">class Foo {
doThing() {
console.log("A");
}
getOtherThing(): number {
return 4;
}
}
</code></pre>
<pre><code class="language-ts bad">class Foo {
doThing() {
console.log("A");
}
getOtherThing(): number {
return 4;
}
}
</code></pre>
<p><a id="features-classes-overriding-tostring"></a></p>
<h5 id="overriding-tostring" class="numbered">Overriding toString</h5>
<p>The <code>toString</code> method may be overridden, but must always succeed and never have
visible side effects.</p>
<p>Tip: Beware, in particular, of calling other methods from toString, since
exceptional conditions could lead to infinite loops.</p>
<p><a id="features-classes-static-methods"></a></p>
<h4 id="static-methods" class="numbered">Static methods</h4>
<h5 id="avoid-private-static-methods" class="numbered">Avoid private static methods</h5>
<p>Where it does not interfere with readability, prefer module-local functions over
private static methods.</p>
<h5 id="avoid-static-method-dynamic-dispatch" class="numbered">Do not rely on dynamic dispatch</h5>
<p>Code <em>should not</em> rely on dynamic dispatch of static