Complete the code to declare a variable with a safe type.
let userName: [1] = "Alice";
Using string ensures the variable only holds text, which is safer than any.
Complete the code to define a function parameter with a safe type.
function greet(name: [1]) { console.log(`Hello, ${name}!`); }Defining the parameter as string ensures only text is accepted, preventing errors.
Fix the error by choosing the correct type for the variable.
let age: [1] = "30";
The value is a string, so the type must be string to avoid type errors.
Fill both blanks to create a safe object type and assign it.
type User = { name: [1]; age: [2]; };
const user: User = { name: "Bob", age: 25 };The name should be string and age should be number for type safety.
Fill all three blanks to safely check a variable's type before using it.
function printAge(age: [1] | [2]) { if (typeof age === [3]) { console.log(`Age is ${age}`); } else { console.log("Invalid age"); } }
The function accepts number or string. The typeof check must compare to the string "string" to be correct.
