AI Hints
Home›TypeScript Tutorial›Generics Practice and keyof
TypeScript · Lesson 59 of 120

Generics Quiz

Test yourself.

⏱ 2 min read▶ Video at 45:39🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Let's review generics. Pause after each question, and answer before I do.

Generics Quiz
From the video: Generics Quiz
  • Generics quiz
  • Pause and think
  • Then check

Question 1

What does a type parameter like T do? It's a placeholder, filled in by each use.

Quick check
A type parameter T is:

Question 2

How do you require T to have a price? T extends an object with a price.

Quick check
Require a price with:

Question 3

What's the problem with any instead of generics? The type is lost.

Quick check
Using any instead of T:

Question 4

How do you give T a default? With an equals sign in the angle brackets.

Quick check
A default type parameter:

Summary

Well done. Next, a new section on type operators, starting with keyof.

Key takeaways
  • <T>
  • extends
  • = default
Next up: keyof — the keys as a type