Master TypeScript Quickly;👇
🔹 Introduction to TypeScript
➡ TypeScript is a typed superset of JavaScript
➡ Adds static typing, interfaces, and compile-time checks
➡ Compiles to plain JavaScript
➡ Useful for large-scale applications and better developer tooling
🔹 Setting Up TypeScript
➡ Install with npm install -g typescript
➡ Initialize project with tsc --init to create tsconfig.json
➡ Compile .ts files with tsc filename.ts
➡ Use ts-node for running TypeScript directly
🔹 Basic Types
➡ number, string, boolean, null, undefined, void, any
➡ Arrays: number[] or Array<number>
➡ Tuples: [string, number]
➡ Enums: enum Direction { Up, Down, Left, Right }
➡ Use type and interface for custom types
🔹 Type Inference and Annotations
➡ TypeScript infers types from assigned values
➡ Explicitly declare types for function parameters and return types
➡ Example: let count: number = 5
➡ Helps catch bugs before runtime
🔹 Functions
➡ Type parameters: (a: number, b: number): number
➡ Optional parameters: b?: string
➡ Default values: (x: number = 10)
➡ Arrow functions: const sum = (a: number, b: number): number => a b
🔹 Interfaces & Types
➡ Define object shapes using interface or type
➡ Interfaces can extend other interfaces
➡ Readonly properties: readonly name: string
➡ Optional properties: age?: number
➡ Use | for union types, & for intersection types
🔹 Classes
➡ Support for public, private, protected, and readonly modifiers
➡ Constructors must define parameter types
➡ Class inheritance using extends
➡ Interfaces can be implemented with implements
➡ Static members shared across instances
🔹 Generics
➡ Allow defining reusable components
➡ Example: function identity<T>(arg: T): T
➡ Work with arrays, functions, classes, and interfaces
➡ Add type flexibility without losing type safety
🔹 Type Utilities
➡ Partial<T>, Required<T>, Readonly<T>, Pick<T, K>, Omit<T, K>
➡ keyof, typeof, infer, extends for advanced typing
➡ Type guards using typeof, instanceof, or custom checks
🔹 Modules and Namespaces
➡ Use export and import to share code across files
➡ ES Modules: import { MyFunc } from './utils'
➡ Namespaces group code in the same file (less common in modern TypeScript)
🔹 Asynchronous Programming
➡ Use Promise<T> for async operations
➡ async/await works like in JavaScript
➡ Catch errors with try/catch blocks
🔹 DOM and Browser Types
➡ TypeScript includes built-in types for DOM elements
➡ Example: let btn = document.querySelector('button') as HTMLButtonElement
➡ Helpful for web development with precise element types
🔹 Configuration with tsconfig.json
➡ Customize compiler settings
➡ Key fields:
➡ target, module, strict, baseUrl, paths
➡ Enables strict typing and module resolution
🔹 Working with Third-Party Libraries
➡ Use DefinitelyTyped for types: npm install --save-dev @types/lodash
➡ Helps you use popular JS libraries with TypeScript
🔹 Best Practices
➡ Prefer explicit types for public APIs
➡ Use unknown instead of any when unsure
➡ Modularize code using interfaces and generics
➡ Avoid unnecessary type assertions
📘 For a complete TypeScript ebook with examples, explanations, and real-world projects, get your copy here:
➡
codewithdhanian.gumroad.com/…