Complete the code to define a type predicate function that checks if a value is a string.
function isString(value: unknown): value is string {
return typeof value === [1];
}The typeof operator returns a string describing the type. To check for strings, use "string".
Complete the code to use the type predicate function in a conditional statement.
function printLength(value: unknown) {
if ([1](value)) {
console.log(value.length);
} else {
console.log("Not a string");
}
}The function isString is the type predicate that checks if value is a string.
Fix the error in the type predicate function that checks if a value is an array of numbers.
function isNumberArray(value: unknown): value is number[] {
return Array.isArray(value) && value.every(item => typeof item === [1]);
}Each item in the array must be checked to be of type number using typeof item === "number".
Fill both blanks to create a type predicate that checks if a value is an object with a 'name' property of type string.
function hasNameProperty(value: unknown): value is [1] { return typeof value === [2] && value !== null && 'name' in value && typeof (value as any).name === 'string'; }
The type predicate returns true if value is an object (TypeScript type) and the runtime typeof is "object".
Fill all three blanks to create a type predicate that checks if a value is a function with a 'length' property greater than 1.
function isFunctionWithLength(value: unknown): value is [1] { return typeof value === [2] && value !== null && (value as Function).length [3] 1; }
The type predicate checks if value is a Function type, the runtime typeof is "function", and the function's length property is greater than 1.