TypeScript Basics

In this stage you learn the core syntax and features of TypeScript, a strongly typed superset of JavaScript developed by Microsoft.

TypeScript adds static typing, better tooling, and improved code maintainability, which makes it widely used with frameworks like React, Angular, and NestJS.


1. Installing TypeScript

First install TypeScript globally using Node.js package manager.

npm install -g typescript

Check installation:

tsc --version

2. TypeScript Compilation

TypeScript code (.ts) must be compiled into JavaScript.

Example file:

hello.ts

Compile:

tsc hello.ts

This creates:

hello.js

3. Type Annotations

TypeScript allows you to define types explicitly.

Example:

let name: string = "Akshay";
let age: number = 30;
let isActive: boolean = true;

Benefits:

  • Compile-time error detection
  • Better IDE support
  • Safer code

4. Basic Data Types

Important built-in types:

TypeExample
string"Hello"
number10
booleantrue
array[1,2,3]
tuple[1,"John"]
anyany type
unknownsafer version of any
voidno return value
neverfunction never returns

Example:

let numbers: number[] = [1,2,3];

5. Type Inference

TypeScript can automatically infer types.

Example:

let city = "Delhi";

TypeScript automatically detects it as:

string

6. Arrays

Define arrays with specific types.

Example:

let fruits: string[] = ["Apple","Banana"];

Alternative syntax:

let numbers: Array<number> = [1,2,3];

7. Tuples

Tuples allow fixed number of elements with specific types.

Example:

let user: [number, string];

user = [1,"Akshay"];

8. Enums

Enums represent a set of named constants.

Example:

enum Role {
 Admin,
 User,
 Guest
}

let role: Role = Role.Admin;

9. Any Type

any disables type checking.

Example:

let data: any = "Hello";

data = 10;

âš  Avoid using any in large applications.


10. Unknown Type

unknown is a safer alternative to any.

Example:

let value: unknown = "Hello";

if(typeof value ==="string"){
 console.log(value.toUpperCase());
}

11. Void Type

Used when a function does not return a value.

Example:

function logMessage(): void {
 console.log("Hello");
}

12. Never Type

Represents values that never occur.

Example:

function throwError(): never {
 throw new Error("Something went wrong");
}

13. TypeScript Functions

Functions can have typed parameters and return types.

Example:

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

14. Optional Parameters

Use ? for optional parameters.

Example:

function greet(name?: string){
 console.log(name);
}

15. Default Parameters

Example:

function greet(name: string = "Guest"){
 console.log(name);
}

16. Basic Interfaces

Interfaces define object structures.

Example:

interface User {
 id: number;
 name: string;
}

const user: User = {
 id: 1,
 name: "Akshay"
};

What You Should Know After Stage 2

You should understand:

  • Type annotations
  • Data types
  • Arrays and tuples
  • Enums
  • Functions with types
  • Interfaces basics

Recommended Learning Time

TopicTime
TypeScript setup1 hour
Data types1 day
Functions1 day
Interfaces1 day

Total: 2–3 days

Important for Interview

never vs void vs unknown vs any

any: opt-out of type safety (discouraged).

never: no value ever. Function never returns or an impossible type.

void: absence of a useful value (e.g., a function that returns but returns nothing).

unknown: some value, but type not known yet (must narrow before using).

function f1(): never { throw new Error("boom"); } // never returns
function f2(): void { console.log("ok"); }        // returns undefined
let a: unknown = JSON.parse("{}");                // must narrow
let b: any = getFromLegacyLib();                  // unsafe: anything goes
Share with