In TypeScript, a tuple is a type that represents an array with a fixed number of elements, where each element can have a different type. The order of the types in the tuple definition corresponds to the order of the values in the actual array. Tuples are similar to arrays, but they have a specific structure and can be used to model finite sequences with known lengths.
You can define a tuple type by specifying the types of its elements enclosed in square brackets, separated by commas:
let myTuple: [string, number] = ["hello", 42];This declares a variable myTuple of type [string, number], which means it's an array with two elements: the first element must be a string, and the second element must be a number. You can access individual elements of a tuple using indexing syntax:
let myTuple: [string, number] = ["hello", 42];
console.log(myTuple[0]); // "hello"
console.log(myTuple[1]); // 42Note that you can also use destructuring syntax to extract individual elements from a tuple:
let myTuple: [string, number] = ["hello", 42];
let [first, second] = myTuple;
console.log(first); // "hello"
console.log(second); // 42Tuples are useful when you need to represent a fixed set of values where each value has a different type. For example, you might use a tuple to represent a 2D point with x and y coordinates:
let point: [number, number] = [10, 20];Or you might use a tuple to represent a person's name and age:
let person: [string, number] = ["John Smith", 30];In TypeScript, an enum is a way to define a set of named constants. Enums allow you to define a collection of related values that can be used interchangeably in your code.
For example, let's say you're building a weather app and you want to define a set of possible weather conditions like "sunny", "cloudy", "rainy", and "snowy". You could define an enum like this:
enum WeatherConditions {
Sunny,
Cloudy,
Rainy,
Snowy,
}In this example, WeatherConditions is the name of the enum, and each of the values (e.g. Sunny, Cloudy, etc.) is assigned an automatic numerical value starting from 0. You can also assign specific values to each enum member like this:
enum WeatherConditions {
Sunny = "sunny",
Cloudy = "cloudy",
Rainy = "rainy",
Snowy = "snowy",
}In this case, each enum member is explicitly given a string value.
You can use enums in your code by referring to them by their name, for example:
const currentWeather = WeatherConditions.Sunny;
console.log(`The current weather is ${currentWeather}`);
// Output: The current weather is SunnyEnums are a useful tool for creating more readable and maintainable code by providing a way to define a set of related values with clear, meaningful names.