InterviewHubPRO
Section Overview
Q258

Question 258 of 521

Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

50%
JavaScript Internals (521 Qs)•Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

What are the advantages of typescript over javascript

Below are some of the advantages of typescript over javascript,

  1. TypeScript is able to find compile time errors at the development time only and it makes sures less runtime errors. Whereas javascript is an interpreted language.
  2. TypeScript is strongly-typed or supports static typing which allows for checking type correctness at compile time. This is not available in javascript.
  3. TypeScript compiler can compile the .ts files into ES3,ES4 and ES5 unlike ES6 features of javascript which may not be supported in some browsers.

Practical Code Example

typescript
// Advantages of TypeScript:
// 1. Compile-time error catch before production
// 2. Rich IDE autocompletion and refactoring tools
// 3. Self-documenting interfaces and types:
type UserRole = 'ADMIN' | 'EDITOR' | 'VIEWER';
function authorize(role: UserRole) {
  // Autocompletes exact strings, rejects invalid roles!
}