TypeScript is a programming language developed by Microsoft that builds on JavaScript by adding static type definitions. It's a "superset" of JavaScript: all JavaScript code is valid TypeScript, but TypeScript adds additional features for catching errors during development.
Why TypeScript Exists
JavaScript is flexible but error-prone. You can accidentally pass a string where a number is expected, and you won't know until the code runs (and crashes). TypeScript catches these errors while you're writing code.
How It Works
TypeScript code is "compiled" (technically "transpiled") to JavaScript before it runs. The type checking happens during this step:
// TypeScript catches this error before running
function greet(name: string) {
return "Hello, " + name;
}
greet(123); // Error: Argument of type 'number' is not assignable to parameter of type 'string'
Key Features
- Type Annotations: Declare what types variables and functions expect
- Interfaces: Define the shape of objects
- Generics: Create reusable components that work with multiple types
- Enums: Define sets of named constants
- IDE Support: Autocomplete, refactoring, documentation
TypeScript vs. JavaScript
| TypeScript | JavaScript | |------------|------------| | Catches errors at compile time | Catches errors at runtime | | Requires compilation step | Runs directly | | Better tooling support | More flexible | | Steeper learning curve | Easier to start |
Who Uses TypeScript
TypeScript has become the standard for large-scale JavaScript projects:
- Microsoft (obviously)
- Google (Angular is TypeScript-first)
- Airbnb, Slack, Asana
- Most modern React projects
Should You Use TypeScript?
For small scripts, JavaScript is fine. For applications with multiple developers or complex logic, TypeScript's safety and tooling make it worth the overhead.