TypeScript logoTypeScript v5.0INTERMEDIATE

TypeScript

TypeScript cheat sheet covering types, interfaces, generics, utility types, type guards, and advanced type system features with examples.

10 min read
typescripttypesinterfacesgenericsjavascript

Basic Types

Primitive Types

TypeScript basic primitive types and type annotations

typescript
💡 Use unknown instead of any when possible
📌 const assertions create readonly literal types
✅ Enable strictNullChecks for better null safety

Type Aliases & Unions

Create custom type aliases and union types for flexibility

typescript
💡 Use discriminated unions for exhaustive checks
⚡ Type aliases improve code readability
📌 Template literal types for string manipulation

Functions

Function Types

Type annotations for functions and their parameters

typescript
💡 Use optional parameters instead of overloads when possible
📌 Arrow functions preserve lexical this
✅ Always type function parameters

Function Overloading

Define multiple function signatures for different parameter types

typescript
⚠️ Overload signatures must be compatible with implementation
💡 Order overloads from most specific to least specific
📌 Consider union types instead of overloads

Interfaces & Classes

Interfaces

Define object shapes and contracts for type checking

typescript
💡 Interfaces can be extended and merged
📌 Use interfaces for object shapes
✅ Prefer interfaces over type aliases for objects

Classes

Object-oriented programming with TypeScript classes

typescript
💡 Use parameter properties to reduce boilerplate
🔒 Private fields start with # in modern TS
📌 Abstract classes cannot be instantiated

Inheritance

Class inheritance and method overriding in TypeScript

typescript
💡 Use super() to call parent constructor
📌 Protected members accessible in derived classes
✅ Mixins provide multiple inheritance pattern

Generics

Generic Functions

Create reusable functions that work with multiple types

typescript
💡 TypeScript often infers generic types automatically
📌 Use constraints to limit generic types
✅ Generics make code reusable and type-safe

Generic Classes & Interfaces

Build flexible classes and interfaces with generic types

typescript
💡 Generic classes create type-safe data structures
📌 Static methods can have their own generic parameters
✅ Use generic constraints for type safety

Advanced Types

Utility Types

Built-in TypeScript utility types for type transformations

typescript
💡 Utility types reduce boilerplate code
⚡ Combine utility types for complex transformations
📌 Most utility types work with generics

Type Guards

Runtime checks that narrow types within conditional blocks

typescript
💡 Type guards narrow types in conditional branches
📌 Custom type guards use "is" syntax
✅ Assertion functions throw or narrow types

Mapped & Conditional Types

Transform and conditionally create types based on other types

typescript
💡 Mapped types transform properties of existing types
⚡ Conditional types enable type-level programming
📌 infer keyword extracts types in conditions

Modules & Namespaces

Imports & Exports

ES6 module syntax for importing and exporting code

typescript
💡 Use type-only imports for better tree-shaking
📌 Dynamic imports for code splitting
✅ Re-export to create public API

Declaration Files

Type declarations for JavaScript libraries and modules

typescript
💡 Use @types packages for library types
📌 .d.ts files contain only type declarations
✅ Declare modules for assets and untyped packages

TSConfig Reference

Compiler Options Reference

Complete reference of TypeScript compiler options with descriptions and values

typescript
💡 Start with strict: true and adjust as needed
📌 Use paths for clean import aliases
⚡ skipLibCheck speeds up compilation significantly
✅ Different project types need different configs