Skip to content
intermediatePhase 35 · TypeScript

TypeScript Fundamentals

Set up TypeScript, understand type annotations, and compile to JavaScript.

45m
0 problems
Topic Progress0%

What is TypeScript

What is TypeScript

TypeScript is a statically typed superset of JavaScript developed by Microsoft. It adds optional type annotations and other features to JavaScript, then compiles down to plain JavaScript.

Key Benefits

  • Catch errors at compile time instead of runtime
  • Better IDE support with autocomplete and refactoring
  • Self-documenting code through type annotations
  • Safer refactoring with type checking

How TypeScript Works

// TypeScript code
function greet(name: string): string {
  return `Hello, ${name}!`;
}

// Compiles to JavaScript
function greet(name) {
  return `Hello, ${name}!`;
}

TypeScript doesn't change how your code runs—it only adds a layer of type safety during development.

Setting Up TypeScript

Setting Up TypeScript

Installation

# Install TypeScript globally
npm install -g typescript

# Or as a project dependency
npm install --save-dev typescript

# Verify installation
tsc --version

tsconfig.json

The TypeScript configuration file controls how the compiler behaves:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "./dist",
    "rootDir": "./src"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

Key Compiler Options

Option Description
strict Enables all strict type-checking options
target JavaScript version to compile to
module Module system to use
outDir Output directory for compiled files

Type Annotations

Type Annotations

Type annotations tell TypeScript what type a variable, parameter, or return value should be.

Variable Annotations

// Explicit type annotation
let age: number = 25;
let name: string = "Alice";
let isActive: boolean = true;

// Type inference (TypeScript figures it out)
let count = 10; // inferred as number
let greeting = "hello"; // inferred as string

Function Annotations

// Parameter and return types
function add(a: number, b: number): number {
  return a + b;
}

// Arrow function
const multiply = (x: number, y: number): number => x * y;

// Void return type
function log(message: string): void {
  console.log(message);
}

Object Annotations

interface User {
  name: string;
  age: number;
  email?: string; // optional
}

const user: User = {
  name: "Bob",
  age: 30
};

Compiling TypeScript

Compiling TypeScript

TypeScript must be compiled to JavaScript before it can run in browsers or Node.js.

Basic Compilation

# Compile a single file
tsc app.ts

# Compile all files in a project
tsc

# Watch mode (recompile on changes)
tsc --watch

Compilation Output

Given this TypeScript:

// src/app.ts
const message: string = "Hello";
console.log(message);

With outDir: "./dist", TypeScript generates:

// dist/app.js
const message = "Hello";
console.log(message);

Using with Build Tools

Most projects use build tools instead of running tsc directly:

# With Vite
npm run dev

# With webpack
npm run build

# With esbuild
npm run build

These tools handle TypeScript compilation along with bundling, minification, and other optimizations.

Practice Problems

0/3solved
Build TypeScript Fundamentals Component

Create a reusable React component implementing TypeScript Fundamentals. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
TypeScript Fundamentals Testing

Write unit and integration tests for TypeScript Fundamentals using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
TypeScript Fundamentals Performance

Optimize TypeScript Fundamentals for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What is TypeScript?

Question 1 options

2. What file configures the TypeScript compiler?

Question 2 options

3. Which compiler option enables all strict type-checking?

Question 3 options

4. What does TypeScript compile to?

Question 4 options

Flashcards

Question

What is TypeScript?

Answer

A statically typed superset of JavaScript that compiles to plain JavaScript

Question

What is the TypeScript config file called?

Answer

tsconfig.json

Question

What does the 'strict' compiler option do?

Answer

Enables all strict type-checking options

Question

What command compiles TypeScript in watch mode?

Answer

tsc --watch

Question

What is TypeScript Fundamentals?

Answer

TypeScript Fundamentals is a key concept in frontend development.

Revision Notes

Key Takeaways

  • 1.TypeScript adds static types to JavaScript
  • 2.TypeScript compiles to plain JavaScript
  • 3.tsconfig.json configures the compiler
  • 4.Type annotations help catch errors early
  • 5.TypeScript improves IDE support and refactoring

Interview Tips

  • Explain that TypeScript is a superset of JavaScript, not a replacement
  • Discuss the benefits: compile-time error checking, better tooling, self-documenting code
  • Mention that TypeScript compiles to JavaScript and adds no runtime overhead

Cheat Sheet

Cheat Sheet

TypeScript Setup

npm install -g typescript
tsc --version
tsc --init  # creates tsconfig.json

Basic Type Annotations

let name: string = "Alice";
let age: number = 25;
let active: boolean = true;

Function Types

function add(a: number, b: number): number {
  return a + b;
}

Key Compiler Options

  • strict: Enable all strict checks
  • target: JS version to compile to
  • outDir: Output directory
  • rootDir: Source directory