Skip to main content
TypeScript is a statically typed superset of JavaScript. Every valid .js file is already valid TypeScript, you layer types on top incrementally.
  1. TypeScript compiles down to plain JavaScript, so the runtime (browser or Node.js) never sees a .ts file.
  2. It adds a compile step between writing code and running it. That step is where all type errors are surfaced.
  3. It does not change JavaScript’s runtime behavior.
  4. The types exist only at compile time, they are completely erased in the emitted JavaScript. They have zero runtime cost.
TypeCheck.ts

Checking JavaScript with tsc

You do not have to migrate to .ts files to get TypeScript’s benefits.
  1. --checkJs enables type checking in .js files.
  2. --noEmit only performs type-checking, does not output any compiled .js files.
  3. --strict enables all strict type-checking options (recommended).
    • It is a shorthand that enables strictNullChecks, noImplicitAny, strictFunctionTypes, and several others at once.
[!WARNING] watch mode watch mode recompiles on file changes, but it only watches .ts files.
  1. With a tsconfig.json present, just run tsc -w from the project root to watch all files.

tsx

tsx is a fast TypeScript runner built on esbuild. It runs .ts and .tsx files directly without a separate compile step.
  1. Useful for scripts, CLI tools, and quick prototyping.

tsconfig.json

The tsconfig.json file controls how TypeScript compiles your project.
  1. You can create it manually or run tsc --init to generate a default one.
Expandable tsconfig.json
  1. You can learn about all the options in the TypeScript Handbook, or check this cheatsheet on Total Typescript.

Type Annotations

You annotate variables, parameters, and return types with : type.
  1. TypeScript can also infer types, you don’t always have to annotate.

Literal Types

Instead of a broad type like string, a literal type restricts a value to one specific value.
  1. These are especially useful in function parameters to restrict allowed values.

Strict Null Checks

Without strictNullChecks, null and undefined are assignable to any type, a common source of runtime bugs.
  1. With it enabled, you must explicitly handle null and undefined.
  1. To allow null, use a union type:

Union Types

A union type means a value can be one of several types.

Type Narrowing and Type Guards

TypeScript tracks types through conditional branches, this is called narrowing.
  1. A type guard is any expression that narrows the type in a scope.

Type Aliases and Interfaces

Both let you name a type, but they have different strengths.
[!WARNING] Usage Use a type alias for primitives and interface for object shapes, but this is a convention, not a rule.
  1. Type aliases can represent any type, including unions and primitives, while interfaces are primarily for object shapes.
  1. Interfaces can extend other interfaces, while type aliases use intersections.
  1. Declaration merging interfaces with the same name are merged automatically.

Promises

Promises in TypeScript are generic, you declare what they resolve to.
[!WARNING] Return Types TypeScript will infer the return type in most cases, but being explicit is useful.

Generics

Generics let you write reusable code that works with any type while preserving type safety.
  1. You can constrain generics with extends.

Utility Types

TypeScript ships built-in generic types for common transformations.
  1. Readonly<T> makes all properties immutable.
  1. Partial<T> makes all properties optional, useful for update//patch operations.
  1. Pick<T, K> constructs a type with only the specified keys.
  1. Omit<T, K> is the opposite of Pick, builds a type without the specified keys.

any & unknown

any opts a value completely out of the type system. Use it as a last resort.
  1. If you find yourself reaching for any, consider unknown first.
    1. Both can hold any value, but unknown is safer, you must narrow it before using it.

Index Access Types

Extract the type of a property using bracket notation on a type.

Type Assertions

Type assertions tell the compiler to treat a value as a specific type, using as.
[!WARNING] Unsafe Assertions as does not perform runtime checks. If you’re wrong, you get a runtime error, not a compile error. Use type guards when possible.

DefinitelyTyped

Many npm packages are written in JavaScript and don’t include types. The community publishes type definitions under the @types scope on npm.
  1. These come from the DefinitelyTyped repository.

Zod

Zod is a schema declaration and validation library. It bridges the gap between TypeScript’s static types (compile-time) and runtime data validation.
  1. TypeScript types are erased at runtime, they can’t validate an API response.
  2. Zod schemas validate at runtime and infer the TypeScript type automatically.