BudiBadu Logo
Samplebadu
TypeScript

Learn TypeScript by Examples

5.x

TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.

These examples cover the core concepts of TypeScript, from basic types and interfaces to advanced features like generics and type guards.

Types

Comprehensive guide to TypeScript basic types including primitives, arrays, and tuples. This sample code demonstrates boolean, number, string, array notations, tuple structure, and the any type for flexible typing in your TypeScript applications.

Interfaces

Learn how to define object structures using TypeScript interfaces with this code example. Covers required properties, optional fields using the question mark syntax, readonly modifiers for immutability, and enforcing type contracts across your codebase.

Enums

Explore TypeScript enumerations for organizing related constants with this sample code. Demonstrates both numeric enums with auto-incrementing values and string enums for readable runtime output, essential patterns for managing configuration and state values.

Generics

Master TypeScript generics to write reusable, type-safe code with this comprehensive code example. Shows generic functions, interfaces, explicit type arguments, and constraint syntax to create flexible components that work across multiple types while preserving type information.

Functions

Understanding TypeScript function signatures and parameter handling through practical sample code. Covers required parameters, optional parameters with the question mark, default parameter values, rest parameters for variable arguments, and return type annotations.

Classes

Object-oriented programming in TypeScript with access modifiers and inheritance patterns. This code example illustrates public, private, and protected members, class extension with the extends keyword, constructor chaining with super, and method overriding for specialized behavior.

Modules

Organizing TypeScript code with ES6 module imports and exports in this practical sample code. Demonstrates export interface, export const, export class, and named import syntax for creating modular, maintainable applications with explicit dependency management.

Unions

Combining multiple types with TypeScript union syntax through detailed code examples. Shows how to create flexible parameters using the pipe operator, implement type guards with typeof checks, and use string literal unions for restricting values to specific constants.

Intersections

Merging multiple type definitions into unified contracts using intersection types. This sample code demonstrates combining interfaces with the ampersand operator, creating composite types for API responses, and building complex structures from reusable type components.

Type Guards

Narrowing union types safely with TypeScript type guards in this comprehensive code example. Covers built-in typeof and instanceof checks, custom type predicates using the is keyword, and control flow analysis for accessing type-specific properties and methods.