TypeScript includes several basic types which allow developers to specify and validate the data types they expect:
number
: For numerical values.string
: For textual data.boolean
: For true/false values.array
: For arrays of other types.tuple
: For fixed-length arrays with specified types.enum
: For enumerated values.any
: For unknown or dynamic types.void
: For functions that don't return a value.null
andundefined
: For values that are null or undefined.
-
Used for integers and floating-point numbers.
Example:
let age: number = 20; // integer number let price: number = 9.99; // floating-point number let binary: number = 0b1010; // binary number let decimal: number = 6; // decimal number let octal: number = 0o744; // octal number let hex: number = 0xf00d; // hexadecimal number
- tring : Used for text. It must be enclosed within single quotes, double quotes, or backticks..
Example:
let name: string = "Ali Omar"; // double quote
let greeting: string = `Hello, my name is ${name}`; // backticks
-
Used for true or false values (1 or 0).
Example:
let isActive: boolean = true; // true let isDisabled: boolean = false; // false
-
Used for arrays of other types.
Example:
let numbers: number []= [1, 2, 3]; let listOne: number[] = [1, 2, 3]; // same as above let listTwo: Array<number> = [1, 2, 3];// same as above let names: string[] = ['Ali', 'Omar', 'Abdi']; let listThree: Array<string> = ['Ali', 'Omar', 'Abdi']; // same as above
-
Used for fixed-length arrays with elements of specified types.
Example:
let x:[string, number] ; x = ["Hello", 10]; // [string, number] OK // x = [10, "hello"]; // Error
-
Used for creating named constants.
Example:
enum Color {Red, Green, Blue} let c: Color = Color.Green; enum Users {Adim, User, Guest}; let u: Users = Users.User;
-
Used when the type is unknown or dynamic.
Example:
let notSure: any = 6; notSure = "maybe a string instead"; notSure = false;
-
Commonly used as the return type of functions that do not return a value.
Example:
function warnUser(): void { console.log("This is my warning message"); } function logMessage(message: string): void { console.log(message); }
-
Special types that have only one value each (
null
andundefined
).Example:
let u: undefined = undefined; let n: null = null;