Skip to main content

TypeScript

Linting

Add the recommended TypeScript rules to ESLint.

tsconfig.json

Enable strict mode in tsconfig.json:

{
  "compilerOptions": {
    "strict": true
  }
}

ts-node

Set --transpileOnly to lower memory usage when running ts-node in production.

{
  "scripts": {
    "start": "ts-node --transpileOnly src/index.ts"
  }
}

Utility types

Use utility types to simplify your types.

Type aliases

Use type aliases to make your types more readable.

type User = {
  id?: string;
  name: string;
};

type UserWithId = User & { id: string };

Apply the rule of least power

Use the rule of least power to make code more maintainable.

Using more advanced types can prevent users making mistakes, but it can also make the code harder to understand. Use the simplest type that will work for your use case.

When in doubt, KISS applies.

Use unknown instead of any

There are few use cases for any. Use unknown instead. See the TypeScript handbook for more information.

Use ? over !

Use the non-null assertion operator sparingly. It can be useful when you know a value is not null, but it can also hide bugs.

Where possible, use the optional chaining operator instead.

Barrelling

When providing a library, barrel your exports to make the library easier to consume.

Automate the barrelling with Barrelsby.

This page was last reviewed on 25 March 2025. It needs to be reviewed again on 25 March 2026 by the page owner platops-build-notices .
This page was set to be reviewed before 25 March 2026 by the page owner platops-build-notices. This might mean the content is out of date.