What is TypeScript?
TypeScript is a superset of JavaScript that adds static type checking. Every valid JavaScript program is also valid TypeScript, but TypeScript lets you annotate variables, function parameters, and return values with types — catching a whole class of bugs at compile time rather than at runtime.
Why TypeScript Exists
JavaScript was designed for small scripts in web pages. As applications grew to millions of lines, the lack of types made refactoring dangerous and onboarding slow. Microsoft created TypeScript in 2012 to bring type safety to JavaScript while keeping full compatibility. Today it is the default choice for large-scale web projects — Angular, Vue 3, and most major libraries ship TypeScript definitions.
Basic Types
TypeScript includes familiar primitives and structural types:
let name: string = "Alice";
let age: number = 30;
let active: boolean = true;
let tags: string[] = ["dev", "ops"];
let config: { retries: number; timeout: number } = { retries: 3, timeout: 5000 };
The compiler flags type mismatches before the code ever runs. You can format TypeScript configuration files (tsconfig.json) with the JSON Formatter.
Interfaces and Type Aliases
Interfaces describe the shape of objects:
interface User {
id: number;
name: string;
email?: string; // optional
}
Type aliases can describe unions, intersections, and more complex types: type Result = Success | Failure. Both are powerful tools for modelling your domain.
Generics
Generics let you write reusable functions and classes that work with any type while preserving type safety:
function first<T>(items: T[]): T | undefined {
return items[0];
}
The compiler infers T from usage, so first(["a", "b"]) returns string | undefined without you specifying the type explicitly.
The Compiler and tsconfig.json
The TypeScript compiler (tsc) reads tsconfig.json for configuration. Key options include strict (enable all strict checks), target (which JS version to emit), and module (module system). Starting with "strict": true is recommended — it catches the most bugs. Use the Code Diff to compare tsconfig files across projects.
Type Inference
TypeScript infers types from context, so you rarely need to annotate everything:
const count = 42; // inferred as number
const users = [{ name: "A" }]; // inferred as { name: string }[]
Explicit annotations are most useful at function boundaries and public API surfaces.
When to Use TypeScript
TypeScript shines in medium-to-large codebases, team projects, and any code that will be maintained long-term. The upfront cost of adding types pays off in safer refactoring, better IDE support, and self-documenting code. For small scripts, prototypes, or one-off utilities, plain JavaScript is perfectly fine. You can test regular expressions used in both JS and TS with the RegExp Tester.
Migrating from JavaScript
You can adopt TypeScript incrementally. Rename .js files to .ts, set "allowJs": true in tsconfig, and fix type errors one file at a time. Third-party types are available on DefinitelyTyped (@types/... packages). Most teams report that the migration pays for itself within a few months through reduced bugs and faster onboarding.