TypeScript Basic Types
TypeScript Basic Types
Types are the foundation of TypeScript. Every value in a program belongs to a type — a category that defines what kind of data it holds and what operations work on it. TypeScript provides several built-in basic types that cover the most common kinds of data used in any application.
What Is a Type?
Think of a type as a label on a storage box. A box labeled Numbers holds only numbers — putting a name inside it breaks the rule. Types in TypeScript work the same way. Once a variable gets a type, it can only hold that kind of data.
Box labeled "number" Box labeled "string" +------------------+ +------------------+ | 42 | | "Rahul" | | 99.5 | | "Hello" | | -7 | | "TypeScript" | +------------------+ +------------------+ Accepts only numbers Accepts only text
The Basic Types in TypeScript
1. number
The number type holds all numeric values — integers, decimals, negative numbers, and special values like Infinity.
let age: number = 25; let price: number = 99.99; let temperature: number = -10; let population: number = 1_400_000_000; // underscores for readability console.log(age); // 25 console.log(price); // 99.99 console.log(temperature); // -10
2. string
The string type holds text values. Strings can use single quotes, double quotes, or backticks (template literals).
let firstName: string = "Priya";
let city: string = 'Mumbai';
let greeting: string = `Hello, ${firstName}! Welcome to ${city}.`;
console.log(greeting); // Hello, Priya! Welcome to Mumbai.
3. boolean
The boolean type holds only two values: true or false. This type powers decision-making in programs — conditions, switches, and flags.
let isLoggedIn: boolean = true; let hasSubscription: boolean = false; let isAdult: boolean = age >= 18; console.log(isLoggedIn); // true console.log(hasSubscription); // false
4. null
The null type represents an intentional absence of a value. It signals that a variable exists but holds nothing meaningful on purpose.
let selectedItem: null = null; // The user has not selected anything yet
5. undefined
The undefined type represents a variable that has been declared but not yet assigned any value.
let deliveryDate: undefined = undefined; // Delivery date is not known yet
6. any
The any type opts out of type checking completely. A variable of type any can hold any kind of value. Using any too often removes the safety benefits of TypeScript, so use it only when absolutely necessary.
let userData: any = "Amit";
userData = 42; // allowed
userData = true; // allowed
userData = { id: 1 }; // allowed
7. unknown
The unknown type is the safer version of any. A variable of type unknown can hold any value, but TypeScript forces the developer to check the type before using it. This prevents unexpected errors.
let inputValue: unknown = "Hello";
// Must check type before using
if (typeof inputValue === "string") {
console.log(inputValue.toUpperCase()); // HELLO
}
8. never
The never type represents values that never occur. A function that always throws an error or runs forever has a return type of never.
function throwError(message: string): never {
throw new Error(message);
// This function never returns normally
}
9. void
The void type represents the absence of a return value. Functions that perform an action but return nothing use void as their return type.
function showMessage(message: string): void {
console.log(message);
// No return statement needed
}
showMessage("TypeScript is powerful!"); // TypeScript is powerful!
Type Annotation Syntax
The syntax for adding a type to a variable uses a colon after the variable name:
let variableName: typeName = value;
| | |
| | +--- Actual data stored
| +------------- Type label (what kind of data)
+---------------------------- Variable name
All Basic Types at a Glance
| Type | Example Value | When to Use |
|---|---|---|
number | 42, 3.14, -5 | All numeric data |
string | "Hello", 'World' | Text data |
boolean | true, false | Yes/No, On/Off flags |
null | null | Intentional empty value |
undefined | undefined | Variable not yet assigned |
any | Anything | Escape type checking (use rarely) |
unknown | Anything, but check first | Safe alternative to any |
never | No value ever | Functions that never return |
void | No return value | Functions with no return |
Type Errors in Action
TypeScript reports an error immediately when assigning a value of the wrong type to a variable.
let score: number = 100; score = "excellent"; // Error: Type 'string' is not assignable to type 'number' let name: string = "Kavya"; name = 55; // Error: Type 'number' is not assignable to type 'string'
These errors appear inside the code editor before running the code, making it easy to fix mistakes early.
Practical Example with Multiple Types
// Student record using basic types
let studentName: string = "Arjun";
let studentAge: number = 20;
let rollNumber: number = 1045;
let isEnrolled: boolean = true;
let middleName: null = null; // No middle name
let graduationYear: undefined = undefined; // Not decided yet
console.log("Name: " + studentName);
console.log("Age: " + studentAge);
console.log("Roll: " + rollNumber);
console.log("Enrolled: " + isEnrolled);
console.log("Middle Name: " + middleName);
console.log("Graduation Year: " + graduationYear);
// Output:
// Name: Arjun
// Age: 20
// Roll: 1045
// Enrolled: true
// Middle Name: null
// Graduation Year: undefined
