TypeScript Cheatsheet
TypeScript
JavaScript
Types
Development
Complete reference for TypeScript programming language covering types, interfaces, classes, generics, decorators, modules, and modern development practices with React.
Quick Reference
🎯 Types & Interfaces
Basic types, custom types, interfaces, and type assertions
🏗️ Classes & Objects
Classes, inheritance, access modifiers, and decorators
⚡ Generics & Advanced
Generic types, utility types, and advanced patterns
⚛️ React & Modules
TypeScript with React, modules, and modern development
Getting Started
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. It's developed by Microsoft and adds static type definitions to JavaScript.
TypeScript Basics
Key characteristics
- Static typing - Catch errors at compile time instead of runtime
- JavaScript superset - All valid JavaScript is valid TypeScript
- Compile-time checks - Enhanced IntelliSense and refactoring capabilities
- Modern JavaScript features - Full ES6+ support with backward compatibility
- Gradual adoption - Can be introduced incrementally to existing projects
Setting up TypeScript
Basic TypeScript configuration (tsconfig.json)
Basic Types
TypeScript provides several basic types that correspond to JavaScript primitives, plus additional utility types for better type safety.
Primitive Types
Basic type assignments
Type inference
Arrays and Tuples
Array types
Tuple types
Special Types
Any type
Void, never, and object
Enums
Enums allow you to define a set of named constants, making it easier to document intent and create distinct cases.
Numeric and String Enums
Basic enums
Using enums
Const enums
Functions
TypeScript functions support type annotations for parameters, return types, and advanced features like function overloads and generic functions.
Function Types
Function declarations and expressions
Function types and interfaces
Function overloads
Interfaces
Interfaces define the shape of objects, providing a powerful way to define contracts within your code and with code outside of your project.
Basic Interfaces
Object interfaces
Advanced interface features
Interface Inheritance and Implementation
Multiple inheritance
Implementing interfaces
Classes
TypeScript classes provide object-oriented programming features with type safety, access modifiers, and modern ES6+ class syntax.
Basic Classes
Class definition and usage
Parameter properties
Inheritance and Abstract Classes
Class inheritance
Abstract classes
Generics
Generics provide a way to create reusable components that work with multiple types while maintaining type safety and avoiding code duplication.
Basic Generics
Generic functions
Generic interfaces and types
Advanced Generics
Generic classes
Utility types
Modules and Namespaces
TypeScript supports ES6 modules and provides namespaces for organizing code. Modern TypeScript development primarily uses ES6 modules.
ES6 Modules
Export and import
Re-exports
Namespaces
Basic namespaces
TypeScript with React
TypeScript provides excellent support for React development with strong typing for props, state, hooks, and event handlers.
React Component Types
Function components
Class components
React Hooks with TypeScript
useState and useEffect
Custom hooks
Event Handling
Event types
Decorators
Decorators are an experimental feature in TypeScript. Enable them by setting "experimentalDecorators": true
in your tsconfig.json.
Basic Decorators
Class decorators
Method decorators
Parameter decorators
Advanced Types
TypeScript provides powerful advanced type features including mapped types, conditional types, and template literal types for sophisticated type manipulation.
Mapped Types
Basic mapped types
Conditional Types
Basic conditional types
Template Literal Types
String manipulation
Best Practices
Follow these TypeScript best practices for writing maintainable, type-safe code that leverages the full power of the TypeScript compiler.
- Use strict mode - Enable
"strict": true
in tsconfig.json for maximum type safety - Prefer interfaces over type aliases for object shapes that might be extended
- Use type aliases for unions and complex type expressions
- Leverage type inference - don't over-annotate when TypeScript can infer types
- Use const assertions (
as const
) for immutable values and better literal types - Prefer composition over inheritance - use interfaces and mixins
- Use discriminated unions for type-safe state management
- Avoid
any
- useunknown
or proper typing instead - Use utility types like
Partial
,Pick
,Omit
for type transformations - Write type guards for runtime type checking
- Use namespace imports sparingly - prefer named imports for better tree-shaking
Learn More
Explore comprehensive TypeScript documentation and advanced development patterns
Written by
Deepak Jangra
Created At
Wed Jan 15 2025
Updated At
Fri Jun 13 2025