← All Exercises
Union Type Handling
basictypescript
Background
Union types allow variables to hold values of different types, requiring type narrowing for safe access.
Problem
Write a function that accepts a value of type string | number and returns its length (string length or the number itself).
Input
A value of type string | number.
Output
A number representing the string length or the number value itself.
Sample tests
Sample 1
Input: "hello"
Expected: 5
Your solution
Learning objective
Learn union types and type narrowing using typeof checks.