1. Home
  2. /
  3. TypeScript
  4. /
  5. Modal Example

Modal Example - TypeScript Typing CST Test

Loading…

Modal Example — TypeScript Code

Shows opening and closing a modal with proper type-safe event handling.

document.addEventListener('DOMContentLoaded', () => {
	const modal = document.getElementById('modal') as HTMLElement;
	const openBtn = document.getElementById('open') as HTMLButtonElement;
	const closeBtn = document.getElementById('close') as HTMLButtonElement;

	openBtn.addEventListener('click', () => { modal.style.display = 'block'; });
	closeBtn.addEventListener('click', () => { modal.style.display = 'none'; });
});

TypeScript Language Guide

TypeScript is a statically typed superset of JavaScript that enhances developer productivity, scalability, and reliability by adding static types, modern tooling, and advanced language features while compiling to plain JavaScript for any runtime.

Primary Use Cases

  • ▸Large-scale frontend development
  • ▸Backend APIs with Node.js, Deno, or Bun
  • ▸Cross-platform mobile apps with React Native
  • ▸Cloud functions and serverless workloads
  • ▸Library and SDK development
  • ▸Type-safe dev tooling and automation scripts

Notable Features

  • ▸Static typing with strong type inference
  • ▸Interfaces, generics, and advanced type system
  • ▸Decorators and metadata reflection
  • ▸Namespaces and ES module support
  • ▸Union, intersection, and mapped types
  • ▸Excellent tooling via VS Code

Origin & Creator

Created by Microsoft under the leadership of Anders Hejlsberg (creator of C#), first released publicly in 2012. Initially invented to solve JavaScript scalability issues in large applications. Evolved through major versions adding classes, modules, generics, decorators, async/await support, strict mode, union types, mapped types, and powerful type inference.

Industrial Note

TypeScript dominates enterprise-scale frontend engineering, powering frameworks like Angular, Next.js, React, Vue, Bun, Deno, and many internal developer tools at companies such as Microsoft, Google, Airbnb, Shopify, Stripe, and Meta.

More TypeScript Typing Exercises

TypeScript Theme Toggle and CounterTypeScript Form ValidationTypeScript To-Do ListTypeScript Fetch API ExampleTypeScript Tabs NavigationTypeScript Image SliderTypeScript Countdown TimerTypeScript Drag and Drop ExampleTypeScript Modal with Async Data

Practice Other Languages

CReactPythonC++RustKotlinPHPJavaC#RubyMqlCqlN1qlCypherGremlin