Question · JavaScript and TypeScript course · Module 6: TypeScript Basics

Generic Constraints

How do you constrain a generic parameter T to only accept types with an 'id' property?

Answer options

  • function f<T requires { id: string }>(arg: T)
  • function f<T: { id: string }>(arg: T)
  • function f<T implements { id: string }>(arg: T)
  • function f<T extends { id: string }>(arg: T)

You can check the correct answer by solving this task in the CodeWorlds game.