Professional TypeScript Showcase

Explore advanced TypeScript features, live examples, a pro editor and an in-page AI assistant to help you learn faster.

TypeScript Features

Union Types

Combine multiple types into one — great for flexible APIs and safer runtime checks.

Type Guards

Narrow types at runtime so your code can be both dynamic and type-safe.

Optional Chaining

Access nested props safely with `?.` and reduce null-check boilerplate.

Generics

Write reusable functions/components that preserve type information across types.

Type Assertions

Tell the compiler what you know — useful when interfacing with third-party code.

Type Predicates

Create custom runtime checks to refine types in conditional branches.

TypeScript Articles

Understanding Union Types in TypeScript

Union types allow a variable to hold values of different types. They're defined with the pipe |.

// Defining a union type
type ID = string | number;

function printID(id: ID) {
  if (typeof id === "string") {
    console.log(id.toUpperCase());
  } else {
    console.log(id.toFixed(2));
  }
}

Use unions in API responses or helpers where inputs may be one of several types.

Type Guards for Runtime Safety

Type guards refine types during runtime, making code safer and clearer.

// custom type guard
interface Bird { fly(): void; }
interface Fish { swim(): void; }

function isBird(pet: Bird | Fish): pet is Bird {
  return (pet as Bird).fly !== undefined;
}

Use type predicates value is Type to let TypeScript know what branch is active.

Optional Chaining & Nullish Coalescing

Use ?. and ?? to write concise, safe access and defaults.

interface User {
  name: string;
  address?: { city?: string };
}

const city = (user: User) => user.address?.city ?? "Unknown";

Cleaner than long logical checks and helps readability.

Generics: Flexible & Typed

Generics let you write reusable functions and components while preserving type info.

function identity(value: T): T {
  return value;
}

const v = identity(42);

Great for collections, utility functions and generic components.

Type Assertions & Casting

When you know more than the compiler, use assertions carefully to avoid runtime errors.

const input = document.querySelector('#name') as HTMLInputElement;
console.log(input.value);

Prefer narrowing and guards over assertions when possible.

Type Predicates & Advanced Checks

Create robust predicates when you need precise runtime checks.

function isStringArray(value: any): value is string[] {
  return Array.isArray(value) && value.every(v => typeof v === 'string');
}

Useful for runtime validation before processing data.

TypeScript Examples

Union Types Example

Function that accepts both string and number types and returns different results based on the input type.

// union example code appears here

Output:

// Results will appear here

Try TypeScript Yourself

TypeScript Editor (Ace)
Mode: TypeScript (demo compile → JS)
Output
// Output will appear here
Editor compiles TypeScript-like code by removing type annotations for demo only — not production safe.
TypeScript AI (Demo)
Canned helper: quick tips & inserts