TypeScript
/Beginner
What is TypeScript?
Definition
TypeScript is a strongly typed, object-oriented, compiled programming language built on top of JavaScript.
Explain Like I'm New
Imagine JavaScript is a wild west town where anyone can do anything, which leads to a lot of chaos and crashes. TypeScript is the new sheriff. It enforces strict laws (types) before you even run your code, catching outlaws (bugs) before they reach production.
Real World Example
Writing `function add(a: number, b: number)` instead of `function add(a, b)`. If you accidentally try to pass a string like `add(5, 'hello')`, TypeScript yells at you in your code editor immediately.
Common Use Cases
- •Large-scale enterprise applications
- •Teams with multiple developers
- •Refactoring complex codebases safely
Interactive Example
// JavaScript allows this, but it results in NaN // function multiply(a, b) { return a * b; } // multiply(5, "apple"); // TypeScript catches the error immediately in your IDE function multiplyTS(a: number, b: number): number { return a * b; } // Error: Argument of type 'string' is not assignable to parameter of type 'number'. // multiplyTS(5, "apple");
Interview Questions
basic
- Is TypeScript executed in the browser?
intermediate
- What is a superset?
advanced
- How does TypeScript's structural typing differ from nominal typing?