← All Exercises

Type Assertion Safety

basictypescript~10min

Background

Type assertions override TypeScript inferred types and should be used with runtime validation for safety.

Problem

Write a function that takes an unknown value, asserts it is a string after runtime checking, and returns its uppercase version.

Input

A value of type unknown.

Output

An uppercase string if the value is a string, otherwise null.

Sample tests

Sample 1

Input: "hello"
Expected: "HELLO"

Your solution

Learning objective

Learn safe type assertion patterns with runtime validation for unknown values.