JS Guide
HomeQuestionsSearchResources
Search

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstypescript
Next
typescript
junior
basics

What is TypeScript and what benefits does it provide over JavaScript?

typescript
types
static-typing
compilation
basics
Quick Answer

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It adds optional static typing, interfaces, and other features that help catch errors at compile time, improve IDE support, and make code more maintainable.

Detailed Explanation

What TypeScript Is:

  • Superset of JavaScript (all JS is valid TS)
  • Adds optional static type system
  • Compiles to JavaScript for runtime
  • Developed and maintained by Microsoft

Key Benefits:

  1. Catch Errors Early:

    • Type errors found at compile time, not runtime
    • Prevents common bugs (null access, typos, wrong types)
  2. Better IDE Support:

    • Intelligent autocomplete
    • Inline documentation
    • Refactoring tools
  3. Self-Documenting Code:

    • Types describe expected data shapes
    • Easier to understand without reading implementation
  4. Safer Refactoring:

    • Compiler catches breaking changes
    • Confidence when modifying large codebases
  5. Modern Features:

    • ES6+ features with downcompilation
    • Interfaces, enums, generics

Code Examples

Basic type annotations
// JavaScript - no type safety
function add(a, b) {
  return a + b;
}
add(5, '3'); // Returns '53' - silent bug!

// TypeScript - catches the error
function add(a: number, b: number): number {
  return a + b;
}
add(5, '3'); // Error: Argument of type 'string' is not assignable

// Type inference - TS infers types automatically
let count = 0;      // inferred as number
let name = 'Alice'; // inferred as string
let items = [1, 2]; // inferred as number[]

count = 'hello'; // Error: Type 'string' is not assignable to type 'number'

Resources

TypeScript Handbook

docs

TypeScript for JavaScript Programmers

docs

Related Questions

What are the basic types in TypeScript?

junior
types

What is the difference between interfaces and type aliases in TypeScript?

junior
interfaces
Next
What are the basic types in TypeScript?