0
0
Typescriptprogramming~20 mins

Custom type guard functions in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Type Guard Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a custom type guard function
What is the output of the following TypeScript code when run with console.log(isStringOrNumber(42));?
Typescript
function isStringOrNumber(value: unknown): value is string | number {
  return typeof value === 'string' || typeof value === 'number';
}

console.log(isStringOrNumber(42));
Afalse
Bundefined
CTypeError
Dtrue
Attempts:
2 left
💡 Hint
Think about what the function checks for the input value.
🧠 Conceptual
intermediate
1:30remaining
Purpose of custom type guard functions
What is the main purpose of a custom type guard function in TypeScript?
ATo declare a new type alias
BTo convert a variable from one type to another
CTo check the type of a variable at runtime and inform the compiler about it
DTo throw an error if a type is incorrect
Attempts:
2 left
💡 Hint
Think about how TypeScript uses these functions to help with type safety.
🔧 Debug
advanced
2:00remaining
Identify the error in this custom type guard
What error will this TypeScript code produce when compiled?
function isArrayOfStrings(value: any): value is string[] {
  return Array.isArray(value) && value.every(item => typeof item === 'string');
}

const test = isArrayOfStrings(['hello', 5]);
Typescript
function isArrayOfStrings(value: any): value is string[] {
  return Array.isArray(value) && value.every(item => typeof item === 'string');
}

const test = isArrayOfStrings(['hello', 5]);
ANo error, test is false
BTypeError at runtime
Ctest is true
DSyntaxError
Attempts:
2 left
💡 Hint
Check what the function returns for the given input.
📝 Syntax
advanced
1:30remaining
Correct syntax for a custom type guard
Which option shows the correct syntax for a custom type guard function that checks if a value is a boolean?
A
function isBoolean(value: any): boolean {
  return typeof value === 'boolean';
}
B
function isBoolean(value: any): value is boolean {
  return typeof value === 'boolean';
}
C
function isBoolean(value: any): value is bool {
  return typeof value === 'boolean';
}
D
function isBoolean(value: any): value is Boolean {
  return typeof value === 'boolean';
}
Attempts:
2 left
💡 Hint
Remember the exact syntax for type predicates in TypeScript.
🚀 Application
expert
2:30remaining
Using a custom type guard in conditional code
Given the custom type guard below, what will be the output of the code?
interface Cat { meow: () => void; }
interface Dog { bark: () => void; }

type Pet = Cat | Dog;

function isCat(pet: Pet): pet is Cat {
  return (pet as Cat).meow !== undefined;
}

const pet: Pet = { bark: () => console.log('Woof!') };

if (isCat(pet)) {
  pet.meow();
} else {
  pet.bark();
}
Typescript
interface Cat { meow: () => void; }
interface Dog { bark: () => void; }

type Pet = Cat | Dog;

function isCat(pet: Pet): pet is Cat {
  return (pet as Cat).meow !== undefined;
}

const pet: Pet = { bark: () => console.log('Woof!') };

if (isCat(pet)) {
  pet.meow();
} else {
  pet.bark();
}
AWoof!
BError: pet.meow is not a function
CNo output
DWoof! Woof!
Attempts:
2 left
💡 Hint
Check what the type guard returns and which branch runs.