Complete the code to enable strict mode in a TypeScript configuration file.
{
"compilerOptions": {
"[1]": true
}
}Setting "strict": true in tsconfig.json enables all strict type-checking options in TypeScript.
Complete the code to declare a variable that cannot be assigned null or undefined in strict mode.
let username: string = [1];In strict mode, a variable of type string cannot be assigned null or undefined. Assigning a string literal like "Alice" is correct.
Fix the error by completing the function parameter type to avoid implicit any in strict mode.
function greet(name: [1]) {
console.log(`Hello, ${name}!`);
}In strict mode, parameters must have explicit types. Using string for name avoids implicit any errors.
Fill both blanks to create a function that returns a number or undefined, using strict null checks.
function findIndex(arr: number[], target: number): [1] { const index = arr.indexOf(target); return index [2] -1 ? index : undefined; }
The function returns number | undefined to reflect possible absence of the target. The condition uses !== to check if index is not -1.
Fill all three blanks to define a strict mode interface and a function using it.
interface User {
id: [1];
name: [2];
email?: [3];
}
function printUser(user: User) {
console.log(`User: ${user.name} (ID: ${user.id})`);
}The id is a number, name is a string, and email is an optional string. This matches strict typing rules.