Introduction
In this chapter, we will explore the various types available in TypeScript. Types in TypeScript are used to describe the shape and behavior of data. They enable TypeScript to perform static type checking, helping you catch errors early in the development process.
Table of Contents
- Basic Types
-
- Number
- String
- Boolean
- Any
- Void
- Null and Undefined
- Never
- Unknown
- Array Types
- Tuple Types
- Enum Types
- Conclusion
Basic Types
Number
The number
type represents numeric values, including both integers and floating-point numbers.
Syntax
let variableName: number = value;
Example
Here, we declare a variable age
using number
and assign it a value of 25, and another variable pi
with a floating-point value.
let age: number = 25;
let pi: number = 3.14;
console.log(age, pi); // Output: 25 3.14
String
The string
type represents textual data.
Syntax
let variableName: string = value;
Example
Here, we declare a variable name
using string
and assign it a value, and another variable greeting
that uses template literals.
let name: string = "Ravi";
let greeting: string = `Hello, ${name}!`;
console.log(name, greeting); // Output: Ravi Hello, Ravi!
Boolean
The boolean
type represents true or false values.
Syntax
let variableName: boolean = value;
Example
Here, we declare two variables isStudent
and hasGraduated
using boolean
and assign them true and false values, respectively.
let isStudent: boolean = true;
let hasGraduated: boolean = false;
console.log(isStudent, hasGraduated); // Output: true false
Any
The any
type allows a variable to hold any type of value. It is useful for scenarios where the type of a value is not known at compile-time.
Syntax
let variableName: any = value;
Example
Here, we declare a variable randomValue
using any
and assign it different types of values.
let randomValue: any = 42;
randomValue = "Hello";
randomValue = true;
console.log(randomValue); // Output: true
Void
The void
type represents the absence of any value. It is commonly used as the return type of functions that do not return a value.
Syntax
function functionName(): void {
// function body
}
Example
Here, we declare a function logMessage
that takes a string
parameter and logs it to the console. The function does not return any value.
function logMessage(message: string): void {
console.log(message);
}
logMessage("Hello, TypeScript!"); // Output: Hello, TypeScript!
Null and Undefined
The null
and undefined
types represent the absence of a value and an uninitialized value, respectively.
Syntax
let variableName: null = null;
let variableName: undefined = undefined;
Example
Here, we declare variables emptyValue
and uninitializedValue
using null
and undefined
types, respectively.
let emptyValue: null = null;
let uninitializedValue: undefined = undefined;
console.log(emptyValue, uninitializedValue); // Output: null undefined
Never
The never
type represents values that never occur, such as the return type of a function that always throws an error or never returns.
Syntax
function functionName(): never {
// function body
}
Example
Here, we declare a function error
that throws an error and never returns.
function error(message: string): never {
throw new Error(message);
}
// error("This is an error"); // Uncommenting this line will throw an error: Uncaught Error: This is an error
Unknown
The unknown
type represents a value that could be any type. It is safer than any
because it forces the developer to perform type checking before using the value.
Syntax
let variableName: unknown = value;
Example
Here, we declare a variable value
using unknown
and then perform type checking before using it.
let value: unknown = "Hello, World!";
if (typeof value === "string") {
console.log(value.toUpperCase()); // Output: HELLO, WORLD!
}
Array Types
An array type represents a collection of values of the same type.
Syntax
let arrayName: type[] = [value1, value2, ...];
Example
Here, we declare an array numbers
of number
type and an array names
of string
type.
let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = ["Ravi", "Ankit", "Priya"];
console.log(numbers, names); // Output: [1, 2, 3, 4, 5] ["Ravi", "Ankit", "Priya"]
Tuple Types
A tuple type represents an array with a fixed number of elements of specific types.
Syntax
let tupleName: [type1, type2, ...] = [value1, value2, ...];
Example
Here, we declare a tuple person
with a string
and a number
type.
let person: [string, number] = ["Ravi", 25];
console.log(person); // Output: ["Ravi", 25]
Enum Types
An enum type is a way to define a set of named constants. It is useful for representing a collection of related values.
Syntax
enum EnumName {
Constant1,
Constant2,
...
}
Example
Here, we declare an enum Direction
with four named constants and use it to set the value of move
.
enum Direction {
Up,
Down,
Left,
Right
}
let move: Direction = Direction.Up;
console.log(move); // Output: 0
Conclusion
In this chapter, we covered the various types available in TypeScript, including basic types, array types, tuple types, and enum types. We provided examples to illustrate how these types work in TypeScript. Understanding these types is essential for writing robust and error-free TypeScript code. In the next chapter, we will delve deeper into TypeScript interfaces and how to use them effectively.
Comments
Post a Comment
Leave Comment