Member-only story

TypeScript Best Practices — Classes, Types, and Overloads

John Au-Yeung
4 min readMay 30, 2020

--

Photo by Patrick Fore on Unsplash

TypeScript is an easy to learn extension of JavaScript. It’s easy to write programs that run and does something. However, it’s hard to account for all the uses cases and write robust TypeScript code.

In this article, we’ll look at the best practices to following when writing code with TypeScript, including member overloads, class instance variables, and restricting types.

Member Overloads Should be Consecutive

If we have member overloads, then they should be consecutive so that we can spot them easily.

For instance, instead of writing:

declare namespace Foo {export function foo(s: string): void;export function foo(n: number): void;export function bar(): void;export function foo(sn: string | number): void;}

We write:

declare namespace Foo {
export function foo(s: string): void;
export function foo(n: number): void;
export function foo(sn: string | number): void;
export function bar(): void;
}

This also applies to interfaces, classes, type aliases, and exports.

--

--

Responses (2)