4 min read
•Question 1 of 48easyWhat is TypeScript?
Understanding TypeScript and its benefits.
What You'll Learn
- What TypeScript is
- Why use TypeScript
- TypeScript vs JavaScript
What is TypeScript?
TypeScript is a strongly typed superset of JavaScript developed by Microsoft. It compiles to plain JavaScript and adds optional static typing.
Key Benefits
| Benefit | Description |
|---|---|
| Type Safety | Catch errors at compile time |
| Better IDE Support | Autocomplete, refactoring |
| Self-Documenting | Types serve as documentation |
| Scalability | Easier to maintain large codebases |
Example
code.tsTypeScript
// JavaScript
function greet(name) {
return "Hello, " + name;
}
// TypeScript
function greet(name: string): string {
return "Hello, " + name;
}
greet(123); // Error: Argument of type 'number' is not assignableTypeScript Compiles to JavaScript
code.tsTypeScript
// TypeScript
const add = (a: number, b: number): number => a + b;
// Compiles to JavaScript
const add = (a, b) => a + b;