1. Home
  2. /
  3. TypeScript
  4. /
  5. Theme Toggle and Counter

Theme Toggle and Counter - TypeScript Typing CST Test

Loading…

Theme Toggle and Counter — TypeScript Code

Demonstrates state management, DOM manipulation, localStorage, and theme toggling using TypeScript with proper typing.

document.addEventListener('DOMContentLoaded', () => {
	const counterEl = document.getElementById('counter') as HTMLElement;
	const incrementBtn = document.getElementById('increment') as HTMLButtonElement;
	const decrementBtn = document.getElementById('decrement') as HTMLButtonElement;
	const resetBtn = document.getElementById('reset') as HTMLButtonElement;
	const toggleThemeBtn = document.getElementById('toggleTheme') as HTMLButtonElement;
	const appEl = document.getElementById('app') as HTMLElement;

	let count: number = parseInt(localStorage.getItem('count') || '0', 10);
	let isDark: boolean = localStorage.getItem('isDark') === 'true';

	const updateUI = (): void => {
		counterEl.textContent = `Counter: ${count}`;
		appEl.className = isDark ? 'dark-theme' : 'light-theme';
		toggleThemeBtn.textContent = `Switch to ${isDark ? 'Light' : 'Dark'} Theme`;
	};

	incrementBtn.addEventListener('click', () => { count++; saveAndUpdate(); });
	decrementBtn.addEventListener('click', () => { count--; saveAndUpdate(); });
	resetBtn.addEventListener('click', () => { count = 0; saveAndUpdate(); });
	toggleThemeBtn.addEventListener('click', () => { isDark = !isDark; saveAndUpdate(); });

	function saveAndUpdate(): void {
		localStorage.setItem('count', count.toString());
		localStorage.setItem('isDark', isDark.toString());
		updateUI();
	}

	updateUI();
});

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 Form ValidationTypeScript To-Do ListTypeScript Modal ExampleTypeScript Fetch API ExampleTypeScript Tabs NavigationTypeScript Image SliderTypeScript Countdown TimerTypeScript Drag and Drop ExampleTypeScript Modal with Async Data

Practice Other Languages

CReactPythonC++RustKotlinPHPJavaC#RubyMqlCqlN1qlCypherGremlin