Find: Wrong Generic Constraint
Find the Mistake Medium
Question
This generic function has a type error. Find the mistake:
Code
function getLength<T extends number>(item: T): number {
return item.length;
}
Hint
Numbers don't have a length property. What types do?
Explanation
The constraint T extends number is wrong — numbers don't have a .length property. The constraint should be T extends { length: number } to accept strings, arrays, or any object with a length property. Fix: function getLength<T extends { length: number }>(item: T): number.
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.