Skip to main content

Basic & Core Types

Assigning Types & Type Inference

TypeScript supports a rich set of data types:

string | number | boolean | null | undefined | array | enum | tuple | any | never | union | interfaces

Primitive Types

TypeDescriptionExample
stringText valueslet name: string = "Ajay"
numberIntegers and floatslet age: number = 23
booleanTrue or falselet isAdult: boolean = true
nullIntentional absence of valuelet val: null = null
undefinedVariable declared but not assignedlet x: undefined = undefined

TypeScript does not provide type inference for null or undefined typed variables — you must be explicit.

Array Type

Use type[] syntax to specify the element type of an array.

let prices: number[] = [175, 42];
let fruits: string[] = ["apple", "orange"];

Key Rules

RuleDetail
Explicit typeAlways declare the type to prevent unintended behavior
Empty arraysWithout an explicit type, TS assumes it will always be empty
Type inferenceTS infers from initial elements, but explicit is better

Examples

// Number array
let prices: number[] = [175, 42];
prices.push(34); // yes
prices.push("hello"); // Error: string not assignable to number

// String array
let fruits: string[] = ["apple", "orange"];
fruits.push("banana"); // yes
fruits.push(true); // Error: boolean not assignable to string

// Mixed array using union types
let mixedArray: (number | string)[] = [1, "two"];
mixedArray.push(3); // yes
mixedArray.push(true); // Error: boolean not assignable to number | string

// Explicitly typed empty array
let numbers: number[] = [];
numbers.push(10); // yes
numbers.push("text"); // no

Any Type

The any type lets a variable hold any type, bypassing TypeScript's type checking entirely.

let notSure: any = 4;
notSure = "could be a string";
notSure = true; // also fine

Use any with caution. It defeats the purpose of TypeScript and can introduce hard-to-find bugs. Avoid it as much as possible.

Object Basics

TypeScript enforces that you only access properties that exist on the object's type.

type Person = {
name: string;
age: number;
isAdult?: boolean; // optional property
};

const p: Person = { name: "Ajay", age: 23 };
console.log(p.name); // yes
console.log(p.email); // Error: property email does not exist

The ? makes a property optional — it can be present or absent without a type error.

Three Ways to Type Objects

// 1. Inline (object literal type) — avoid in production
const p: { name: string; age: number } = { name: "Ajay", age: 23 };

// 2. Using `type`
type Person = { name: string; age: number };

// 3. Using `interface`
interface Person { name: string; age: number; }

Types vs Interfaces

Featuretypeinterface
Works with objectsyesyes
Works with primitives / unionsyesno
Can be extendedWith &With extends
Use in productionPreferredOnly for objects
// type — flexible, works for anything
type ID = string | number;
type Person = { name: string };

// interface — forces object shape only
interface Person { name: string; }

Prefer type over interface in most cases. interface forces you to use only objects and can't be used for a single variable or primitive type.