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,
- 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.
- TypeScript is strongly-typed or supports static typing which allows for checking type correctness at compile time. This is not available in javascript.
- 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!
}