Skip to content

en_US: cont_TypeScript Coding Conventions

Komohachi Fujishiki edited this page Apr 4, 2022 · 1 revision

TypeScript Coding Conventions

Do not omit semicolons ;

This is to avoid Automatic Semicolon Insertion (ASI) hazard.

Ref:

Do not omit curly brackets {}

Bad:

if (foo)
	bar;
else
	baz;

Good:

if (foo) {
	bar;
} else {
	baz;
}

As a special case, you can omit the curly brackets if

  • the body of the if-statement have only one statement and,
  • the if-statement does not have else-clause.

Good:

if (foo) bar;

Make sure that the condition and the body statement are on the same line.

Do not use == when it can simply be replaced with ===

Use only boolean (or null related) values in the condition of an if-statement

Bad:

if (foo.length)

Good:

if (foo.length > 0)

Do not use export default

This is because the current language support does not work well with export default.

Ref:

Bad:

export default function(foo: string): string {

Good:

export function something(foo: string): string {