In TypeScript, the equivalent of a Rust struct is an interface or a class. Interfaces in TypeScript are used to define the structure of an object, specifying the required properties and their types. Classes, on the other hand, can also be used to define the structure of an object with properties and methods. Depending on the specific use case, either an interface or a class can be used to create a structure similar to a Rust struct in TypeScript.
What is a TypeScript type alias?
A TypeScript type alias is a way to create a custom name for a specific type or combination of types. It allows developers to define a new name for an existing type, making their code more readable and maintainable. Type aliases are often used to reuse complex type definitions or to create more descriptive names for types used throughout a codebase.
What is a TypeScript union type?
A TypeScript union type is a type that allows a variable to hold values of multiple different types. It is defined by using the vertical bar "|" to separate the different types that the variable can hold. For example, a variable that can hold either a number or a string would be defined as:
1
|
let myVar: number | string;
|
This allows the variable "myVar" to store either a number or a string value. Union types are useful when a variable may need to hold different types of values at different points in the program.
What is a TypeScript enum?
A TypeScript enum is a way to define a set of named constants. Enums allow developers to create a collection of related values that can be accessed by their names rather than their values. This makes code more readable and maintainable. Enums in TypeScript are similar to enums in other programming languages like Java or C#.
How to implement a type guard in TypeScript?
In TypeScript, a type guard is a function that returns a boolean value and is used to narrow down the type of a variable. Here's an example of how to implement a type guard in TypeScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Define a type guard function function isNumber(value: any): value is number { return typeof value === 'number'; } // Use the type guard function function add(a: any, b: any): number { if (isNumber(a) && isNumber(b)) { return a + b; } else { throw new Error('Both arguments must be numbers'); } } // Test the add function console.log(add(2, 3)); // Output: 5 console.log(add('2', 3)); // Output: Error: Both arguments must be numbers |
In this example, the isNumber
function is a type guard that checks if the input value is a number. When calling the add
function, it uses the isNumber
type guard to ensure that both arguments are numbers before performing the addition operation. If one of the arguments is not a number, an error is thrown.
What is a TypeScript intersection type?
A TypeScript intersection type is a way to combine multiple types into one. It is denoted by using the &
symbol, and represents a type that has all the properties of each individual type. When using an intersection type, a value must satisfy all of the constituent types in order to be considered valid. This can be useful when a type needs to have features from multiple sources.