Skip to main content
TypeScript Bug Hunt Hard +35 XP

Missing Return in Union

TypeScript reports not all code paths return a value.

TypeScriptFind the bug
1function parse(val: string | number): string {
2 if (typeof val === "string") return val;
3}
The bug is on line 3

Select the correct fix

Show answer and explanation

Answer: Both A and B work

When val is a number, the function falls through without returning. Add a return for the number case or a fallback.

Hint: What happens when val is a number?

What this challenge tests

Bug Hunt in TypeScript: control-flow, return.

Related challenges