0
0
Typescriptprogramming~20 mins

Union type syntax and behavior in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Union Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this union type check?
Consider the following TypeScript code. What will be logged to the console?
Typescript
type Result = string | number;

function printResult(value: Result) {
  if (typeof value === 'string') {
    console.log(value.toUpperCase());
  } else {
    console.log(value + 10);
  }
}

printResult('hello');
printResult(5);
AHELLO and 5
B"hello" and 15
CTypeError at runtime
D"HELLO" and 15
Attempts:
2 left
💡 Hint
Check how the typeof operator distinguishes between string and number.
🧠 Conceptual
intermediate
1:30remaining
Which union type allows both string and boolean values?
Which of the following TypeScript union types correctly allows a variable to hold either a string or a boolean?
Atype MyType = string | boolean;
Btype MyType = string & boolean;
Ctype MyType = string, boolean;
Dtype MyType = string || boolean;
Attempts:
2 left
💡 Hint
Union types use the pipe symbol | to combine types.
🔧 Debug
advanced
2:00remaining
Why does this union type assignment cause an error?
Examine the code below. Why does the assignment cause a TypeScript error?
Typescript
type ID = number | string;

let userId: ID = true;
ABecause union types cannot include boolean values.
BBecause 'true' is assignable to 'number | string' but needs explicit casting.
CBecause 'true' is not assignable to type 'number | string'.
DBecause 'userId' must be declared with 'const' instead of 'let'.
Attempts:
2 left
💡 Hint
Check the allowed types in the union and the assigned value type.
Predict Output
advanced
2:30remaining
What is the output of this function with union types and type narrowing?
What will the following TypeScript code output when executed?
Typescript
type Input = string | number | boolean;

function describe(input: Input) {
  switch (typeof input) {
    case 'string':
      return `String: ${input.length}`;
    case 'number':
      return `Number: ${input * 2}`;
    case 'boolean':
      return `Boolean: ${input ? 'yes' : 'no'}`;
  }
}

console.log(describe('abc'));
console.log(describe(4));
console.log(describe(false));
A"String: abc", "Number: 4", "Boolean: false"
B"String: 3", "Number: 8", "Boolean: no"
C"String: 3", "Number: 4", "Boolean: no"
DRuntime error due to missing default case
Attempts:
2 left
💡 Hint
Look at how typeof narrows the type and what each case returns.
🧠 Conceptual
expert
3:00remaining
How does TypeScript handle union types with overlapping properties?
Given these interfaces and union type, what is the type of 'value.name' inside the function?
Typescript
interface A { name: string; age: number; }
interface B { name: string; location: string; }

type AB = A | B;

function printName(value: AB) {
  return value.name;
}
Astring
Bstring | undefined
Cnever
DTypeScript error: Property 'name' does not exist on type 'AB'
Attempts:
2 left
💡 Hint
Check which properties are common to both interfaces in the union.