Predict Generic Inference Output
Predict Output Medium
Question
What does this code print?
Code
function identity<T>(value: T): T {
return value;
}
console.log(identity(42));
console.log(identity("hello"));
console.log(identity([1, 2, 3]));
Hint
identity just returns whatever you pass in. TypeScript infers T from the argument.
Explanation
identity(42) — T is inferred as number, returns 42. identity("hello") — T is string, returns "hello". identity([1, 2, 3]) — T is number[], returns the array. console.log of an array prints comma-separated values. Output: 42, hello, 1,2,3.
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.