Complete the code to check if the variable is a string.
if (typeof value === [1]) { console.log("It's a string!"); }
Using typeof value === "string" narrows the type to string.
Complete the code to narrow the type to number.
function double(input: string | number) {
if (typeof input === [1]) {
return input * 2;
}
return input + input;
}Checking typeof input === "number" narrows input to number, allowing multiplication.
Fix the error in the type narrowing condition.
function isBoolean(value: unknown): boolean {
if (typeof value === [1]) {
return true;
}
return false;
}The correct typeof string for boolean values is "boolean" all lowercase.
Fill both blanks to create a type guard for strings and numbers.
function process(value: string | number) {
if (typeof value === [1]) {
return value.toUpperCase();
} else if (typeof value === [2]) {
return value + 10;
}
}Use typeof value === "string" to narrow to string and typeof value === "number" to narrow to number.
Fill all three blanks to create a type guard that handles string, number, and boolean types.
function describe(value: string | number | boolean) {
switch (typeof value) {
case [1]:
return `String: ${value.toLowerCase()}`;
case [2]:
return `Number: ${value + 1}`;
case [3]:
return `Boolean: ${value ? 'yes' : 'no'}`;
}
}Use "string", "number", and "boolean" to match the types returned by typeof.
