TypeScript, being a superset of JavaScript, includes several primitive types that are the basic building blocks for handling data. These are the fundamental types in TypeScript:
1. number
Represents both integers and floating-point numbers.
Example:
let age: number = 25;
let price: number = 19.99;
2. string
Represents sequences of characters.
Example:
let name: string = "Alice";
let greeting: string = `Hello, ${name}`;
3. boolean
Represents a logical value: true
or false
.
Example:
let isDone: boolean = true;
4. null
Represents an explicitly empty or non-existent value.
Example:
let emptyValue: null = null;
5. undefined
Indicates a variable that has been declared but not initialized.
Example:
let notInitialized: undefined = undefined;
6. symbol
Introduced in ES6, symbol
represents a unique and immutable value, often used for object property keys.
Example:
let uniqueKey: symbol = Symbol("key");
7. bigint
Introduced in ES2020, bigint
represents large integers beyond the number
type’s limit.
Example:
let largeNumber: bigint = 9007199254740991n;
Additional Notes
void
: Used as a return type for functions that do not return a value.any
: Allows any type of value and disables type checking.unknown
: A safer alternative toany
that requires type checking before usage.never
: Represents values that never occur, often used in functions that throw errors or infinite loops.
Summary of Main Primitives
Type | Example | Description |
---|---|---|
number | let x: number = 10 | Represents numeric values (integers and floats). |
string | let s: string = "Hi" | Represents text or character sequences. |
boolean | let b: boolean = true | Represents true or false . |
null | let n: null = null | Represents an explicitly empty value. |
undefined | let u: undefined = undefined | Represents an uninitialized variable. |
symbol | let sym: symbol = Symbol("id") | Represents unique identifiers. |
bigint | let big: bigint = 123n | Represents large integer values. |
These types ensure TypeScript enforces strong typing and helps catch errors early during development.