Create a function that takes an array of numbers as input and returns the sum of all the numbers in the array. Make sure to define the types for the input parameter and return value.
functionsumArray(numbers: number[]): number {
let sum = 0;for (let num of numbers) {
sum += num;
}
return sum;
}
const numbers: number[] = [1, 2, 3, 4, 5];
const result: number = sumArray(numbers);
console.log(result); // Output: 15
Define an interface called Person that has the following properties: name (string), age (number), and email (string). Create an object based on this interface and print out its properties.
interface Person {
name: string;
age: number;
email: string;
}
const person: Person = {
name: "John Doe",
age: 25,
email: "[email protected]",
};
console.log(person.name); // Output: John Doe
console.log(person.age); // Output: 25
console.log(person.email); // Output: [email protected]
Write a function called calculateArea that takes the length and width of a rectangle as parameters and returns its area. Define the types for the input parameters and return value.
const length: number = 5;
const width: number = 10;
const area: number = calculateArea(length, width);
console.log(area); // Output: 50
Implement a generic function called reverseArray that takes an array of any type and returns the reversed version of the array. Test it with different types of arrays (e.g., numbers, strings) to ensure it works correctly.
Create a generic interface called Repository that represents a generic data repository. It should have methods like getAll, getById, create, update, and delete. Define the types for the methods and create a class that implements this interface.
Define a type alias called Coordinates that represents the latitude and longitude of a location. It should be an object with latitude and longitude properties, both of which are numbers. Create a variable of type Coordinates and assign some sample values to it.
Define an enum called Color with values representing different colors (e.g., Red, Green, Blue). Create a function that takes a Color value as input and prints out a corresponding message (e.g., "You selected Red").
enum Color {
Red = "Red",
Green = "Green",
Blue = "Blue",
}
functionprintColorMessage(color: Color): void {
switch (color) {
case Color.Red:
console.log("You selected Red");break;case Color.Green:
console.log("You selected Green");break;case Color.Blue:
console.log("You selected Blue");break;
default:
console.log("Unknown color");
}
}
const selectedColor: Color = Color.Red;
printColorMessage(selectedColor); // Output: "You selected Red"
Create a base class called Shape with a method calculateArea that returns the area of the shape. Extend this class to create subclasses for specific shapes like Rectangle and Circle. Implement the calculateArea method in each subclass and test it with different dimensions.
abstract class Shape {
abstract calculateArea(): number;
}
class Rectangle extends Shape {
constructor(private width: number, private height: number) {
super();
}
calculateArea(): number {
return this.width * this.height;
}
}
class Circle extends Shape {
constructor(private radius: number) {
super();
}
calculateArea(): number {
return Math.PI * this.radius ** 2;
}
}
// Test the classes
const rectangle = new Rectangle(5, 10);console.log(rectangle.calculateArea()); // Output: 50
const circle = new Circle(3);console.log(circle.calculateArea()); // Output: 28.274333882308138
Define two interfaces: Car with properties like make, model, and year, and Driver with properties like name and licenseNumber. Create a function that takes objects of type Car and Driver and returns an object with the combined properties of both types.
Create a function that takes a parameter which can be either a string or an array of strings. If it's a string, return the uppercase version of the string. If it's an array of strings, return an array with each string in uppercase.
Declare a variable with an initial value of null and type it as string. Use type assertion to assign a string value to this variable and then print its length.
let myString: string | null = null;
myString = "Hello, TypeScript!" as string;
console.log(myString.length);
Create a function that takes an input parameter of type unknown. Inside the function, implement type guards to check if the input is of type string or number and perform different operations based on the type.
functionprocessInput(input: unknown): void {
if (typeof input === 'string') {
console.log(`Input is a string: ${input.toUpperCase()}`); } elseif (typeof input === 'number') { console.log(`Input is a number: ${input * 2}`); } else { console.log('Unknown input type'); }}processInput("hello"); // Output: "Input is a string: HELLO"processInput(5); // Output: "Input is a number: 10"processInput(true); // Output: "Unknown input type"
Create a generic function that takes an array of elements and returns the first element of the array. Add a constraint to ensure that the generic type can be compared using the > operator.
Create a function that takes two parameters: one can be either a string or number, and the other can be either a boolean or an array of strings. Implement logic in the function to perform different operations based on the types of the parameters.
Create a generic function called filterArray that takes an array of any type and a predicate function as parameters. The function should return a new array that contains only the elements for which the predicate function returns true. Ensure that the function is flexible enough to work with different types of arrays.