Skip to content

TypeScript Tutorial for Beginners: Types, Interfaces and Setup

A TypeScript tutorial for beginners — why TypeScript, setup, basic types, interfaces and type aliases, functions, unions, generics and using it with React and Node.js.
TypeScript Tutorial for Beginners: Types, Interfaces and Setup — WebDesigningPune guide

Quick answer

The key idea of this TypeScript tutorial: TypeScript is JavaScript with static types. You add type annotations such as name: string, and the TypeScript compiler catches mistakes before your code runs. Install it with npm install -D typescript, create a tsconfig.json with npx tsc --init, then learn basic types, interfaces, union types and generics. Frameworks such as Next.js, Angular and Vite templates support TypeScript out of the box.

Note: Frameworks release often — confirm version-specific details in the official docs.

This TypeScript tutorial starts with an honest warning. TypeScript can feel like extra work at first: more syntax, more errors, more things to learn. But most developers who stick with it for a few weeks never want to go back. The errors TypeScript shows while you type are bugs you would otherwise have found later — in the browser, or worse, in production.

The trick is to adopt it gradually. You do not need advanced types to benefit; basic annotations on functions and data already catch a surprising number of mistakes.

Why use TypeScript?

  • Catches errors such as typos and wrong arguments while you code.
  • Better autocomplete and refactoring in editors like VS Code.
  • Types act as documentation for teams.
  • Widely used in modern frameworks and job listings.
Simple labelled diagram explaining “Why use TypeScript?”

Set up TypeScript

npm install -D typescript
npx tsc --init
npx tsc        # compile .ts files to .js

Recent Node.js versions can also run many .ts files directly by stripping types, and tools like Vite and tsx handle TypeScript automatically.

TypeScript tutorial: basic types

let siteName: string = "My Site";
let visitors: number = 1200;
let isLive: boolean = true;
let tags: string[] = ["html", "css"];
let point: [number, number] = [10, 20];   // tuple

Interfaces and type aliases

Code editor or browser screenshot supporting “Interfaces and type aliases”
interface Post {
  id: number;
  title: string;
  published?: boolean;   // optional
}

type Status = "draft" | "published" | "archived";   // union of literals

const post: Post = { id: 1, title: "Hello TypeScript" };

Functions

function formatPrice(amount: number, currency: string = "USD"): string {
  return new Intl.NumberFormat("en-US", { style: "currency", currency }).format(amount);
}

formatPrice(49);          // "$49.00"
// formatPrice("49");     // Error: string is not assignable to number

Narrowing union types

function printId(id: number | string) {
  if (typeof id === "string") {
    console.log(id.toUpperCase());
  } else {
    console.log(id.toFixed(0));
  }
}

Generics

Screenshot or photo that supports “Generics” in a guide about typescript tutorial
function firstItem<T>(items: T[]): T | undefined {
  return items[0];
}

const first = firstItem(["a", "b"]);   // type: string | undefined

TypeScript with frameworks

Tool How to start with TypeScript
React (Vite) Choose the “React + TypeScript” template
Next.js create-next-app sets up TypeScript by default
Angular TypeScript is built in
Vue Use <script setup lang="ts">
Node.js Use tsx, ts-node or Node’s built-in type stripping

Tip: Turn on "strict": true in tsconfig.json from the start. It catches more bugs and is easier than enabling it later.

Adopting TypeScript gradually

  1. Rename one fileChange a .js file to .ts and fix the errors it reveals.
  2. Type your dataAdd interfaces for API responses and main objects.
  3. Type function inputs and outputsParameters and return types first.
  4. Enable strict modeTurn on "strict": true once the basics are in place.
  5. Convert more filesOne module at a time, testing as you go.

Common beginner errors explained

Error What it usually means
Property does not exist on type The object’s type does not include that property — check spelling or update the type
Object is possibly undefined Check the value exists before using it
Type is not assignable You passed the wrong kind of value
Implicitly has an any type Add a type annotation so TypeScript knows what to expect

Tip: Hover over variables in VS Code to see their inferred types. TypeScript often knows more than you think, so you rarely need to annotate everything.

Continue with Node.js tutorial, JavaScript frameworks and JavaScript guides.

Frequently asked questions

Should I learn JavaScript before TypeScript?

Yes. TypeScript builds on JavaScript, so learn JavaScript fundamentals first, then add types.

Is TypeScript hard to learn?

The basics are quick to learn if you know JavaScript. Advanced types take longer but are not needed at first.

Does TypeScript run in the browser?

Browsers run JavaScript. TypeScript is compiled or stripped to JavaScript before running.

What is the difference between interface and type in TypeScript?

Both describe object shapes. Interfaces can be extended and merged; type aliases can also describe unions and other types.

Do I need to type everything in TypeScript?

No. TypeScript infers many types automatically. Focus on function parameters, return values and data structures.

Can I use TypeScript with WordPress?

Yes, for JavaScript in blocks, themes and plugins. WordPress tooling supports TypeScript for block development.

What is the any type?

any turns off type checking for a value. Use it sparingly, as it removes TypeScript’s protection.


Further reading: MDN’s web development learning area.

First published November 17, 2026 · Last updated November 26, 2026