Complete the code to declare a variable with a type in TypeScript.
let message: [1] = 'Hello, TypeScript!';
In TypeScript, you declare a variable's type after a colon. Here, string is the correct type for text.
Complete the code to define a function with typed parameters in TypeScript.
function greet(name: [1]): string { return `Hello, ${name}!`; }
number or boolean for a text parameter.The parameter name should be a string because it represents text.
Fix the error in the TypeScript code by completing the type annotation.
let count: [1] = 10; count = 'ten'; // Error: Type mismatch
string type but assigning a number.The variable count is assigned a number initially, so its type should be number. Assigning a string later causes an error.
Complete the code to create a typed interface and use it in a function parameter.
interface User { {
name: string;
age: number;
}
function printUser(user: {BLANK_2}}) {
console.log(`${user.name} is ${user.age} years old.`);
}The interface body starts with { and the function parameter type is the interface name User.
Fill all three blanks to write a TypeScript class with a constructor and a method.
class [1] { name: string; constructor(name: [2]) { this.name = name; } greet(): [3] { return `Hello, ${this.name}!`; } }
The class is named Person, the constructor parameter type is string, and the greet method returns a string.