Complete the code to check if the variable value is truthy.
if ([1]) { console.log("Value is truthy"); }
In TypeScript, simply using the variable name in the if condition checks if it is truthy.
Complete the code to narrow the type by checking if input is truthy before accessing its property.
function printLength(input: string | null) {
if ([1]) {
console.log(input.length);
}
}input === null which is true only when input is nullinput.length > 0 without ensuring input is not nullChecking input directly in the if condition narrows the type to string because null is falsy.
Fix the error by completing the condition to narrow data to a truthy value before accessing data.value.
function process(data: { value: number } | null) {
if ([1]) {
console.log(data.value);
}
}data.value !== null causes error if data is nulldata === undefined misses the null caseChecking data !== null ensures data is not null, so accessing data.value is safe.
Fill both blanks to create a dictionary of word lengths only for words longer than 3 characters.
const words = ["apple", "cat", "banana", "dog"]; const lengths = { [1]: [2] for (const word of words) if (word.length > 3) };
The dictionary keys are the words themselves, and the values are their lengths. The condition filters words longer than 3 characters.
Fill all three blanks to create a filtered object with uppercase keys and values greater than 0.
const data = { a: 1, b: 0, c: 3 };
const filtered = { [1]: [2] for (const [[3], v] of Object.entries(data)) if (v > 0) };v as the key variablev.toString() instead of vThe keys are converted to uppercase, values are kept as numbers, and the loop variable is k for keys.