In TypeScript, a data type is used to define the type of value that a variable can hold. By defining a data type, you can ensure that the variable will only store values of that specific type. This helps in improving code quality and catching potential errors at compile-time.
Defining Data Types
To define a data type in TypeScript, you can use the type annotations syntax. Type annotations are added to variables or function parameters using the colon (:
) followed by the desired data type.
let age: number;
let name: string;
let isStudent: boolean;
- number: represents numeric values like integers and floating-point numbers.
- string: represents textual values enclosed in single quotes (
'
) or double quotes ("
). - boolean: represents logical values, either true or false.
Type Inference
TypeScript also supports type inference, which means it can automatically infer the data type based on the assigned value.
// Type inferred as number
let age = 25;
// Type inferred as string
let name = "John Doe";
// Type inferred as boolean
let isStudent = true;
This makes it convenient when you don’t want to explicitly specify the data type, especially when it’s obvious from the assigned value.
Custom Data Types
In addition to primitive data types, TypeScript allows you to define your own custom data types using interfaces, classes, and enums.
Interfaces
An interface is a way to define the structure of an object. It specifies the names and types of properties that an object must have.
interface Person {
name: string;
age: number;
}
let person: Person = {
name: "John Doe",
age: 25
};
Classes
A class is a blueprint for creating objects. It encapsulates data and behavior into a single unit.
class Car {
brand: string;
year: number;
constructor(brand: string, year: number) {
this.brand = brand;
this.year = year;
}
startEngine() {
console.log("Engine started");
}
}
let myCar = new Car("Tesla", 2022);
myCar.startEngine();
Enums
An enum is a way to define a set of named constants. It allows you to define a set of values that can be assigned to a variable.
enum Color {
Red,
Green,
Blue
}
let favoriteColor: Color = Color.Blue;
console.log(favoriteColor); // Output: 2 (index of Blue)
Type Assertion
Sometimes, you may need to tell the TypeScript compiler about the specific type of a value when its type cannot be inferred automatically. This is known as type assertion.
// Type assertion using angle-bracket syntax
let someValue: any = "Hello, TypeScript!";
let strLength1: number = (someValue).length;
// Type assertion using 'as' syntax
let strLength2: number = (someValue as string).length;
Type assertion allows you to override the inferred type and treat a value as a different type. However, it does not perform any runtime type checking.
Conclusion
In TypeScript, defining data types is important for ensuring type safety and catching errors early. By using type annotations or relying on type inference, you can define variables with specific data types.
Additionally, TypeScript provides support for custom data types through interfaces, classes, and enums. Understanding and utilizing these features will greatly enhance your ability to write robust and maintainable code.