What is TypeScript?
TypeScript is a superset (or extension) of the JavaScript language that adds a static typing system and a number of features that simplify development of large projects.
Core idea
TypeScript lets you define data types (for example, string, number, boolean, object, array, function, etc.) before the program runs. This helps catch errors at compile time, rather than at runtime.
javascript
function greet(name: string): string {
return `Hello, ${name}!`;
}
greet('Tim'); // correct
greet(123); // compile error: string expectedHow it works
- TypeScript code (.ts) is transpiled into regular JavaScript (.js).
- The
tsccompiler checks types and converts the code into standard JavaScript that can run in the browser or Node.js.
Key benefits
- Early error detection - the IDE and compiler point out where types are violated.
- Smarter autocomplete - editors like VSCode understand the code structure better.
- Support for modern JS features - TypeScript compiles down to older JavaScript versions when needed.
- Better API design - types help document interfaces and classes.
- JavaScript compatibility - any JS code is valid TypeScript.
Key features
- Interfaces (
interface) - Generics
- Modules and namespaces
- Enumerations (
enum) - Decorators
- Union (
|) and intersection (&) types - Type narrowing
- Utility types (
Partial,Pick,Record, etc.)
Comprehensive usage example
javascript
interface User {
id: number;
name: string;
isAdmin?: boolean; // optional field
}
function printUser(user: User): void {
const role = user.isAdmin ? 'admin' : 'user';
console.log(`${user.name} - ${role}`);
}
const user: User = { id: 1, name: 'Tim' };
printUser(user);Summary
TypeScript is a tool for improving the reliability and convenience of JavaScript development. It makes code predictable, safe, and self-documenting, especially in large projects.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.