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
| Type | Description | Example |
|---|---|---|
string | Text values | let name: string = "Ajay" |
number | Integers and floats | let age: number = 23 |
boolean | True or false | let isAdult: boolean = true |
null | Intentional absence of value | let val: null = null |
undefined | Variable declared but not assigned | let 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
| Rule | Detail |
|---|---|
| Explicit type | Always declare the type to prevent unintended behavior |
| Empty arrays | Without an explicit type, TS assumes it will always be empty |
| Type inference | TS 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
| Feature | type | interface |
|---|---|---|
| Works with objects | yes | yes |
| Works with primitives / unions | yes | no |
| Can be extended | With & | With extends |
| Use in production | Preferred | Only 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.