Find: Incorrect Type Assertion
Find the Mistake Medium
Question
This code has a logical type error. Find the mistake:
Code
const value: unknown = 42;
const str: string = value as string;
console.log(str.toUpperCase());
Hint
The assertion compiles but will fail at runtime. Why?
Explanation
The assertion value as string is incorrect because value is actually the number 42. TypeScript allows the assertion (since value is unknown), but at runtime str.toUpperCase() will throw a TypeError because 42 is not a string. Fix: check the type first with typeof value === 'string' before asserting.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.