What is the type of the following expression? function identity(arg: T): T { return arg; } const result = identity(42);

TypeScript Associate Hard

TypeScript Associate — Hard

What is the type of the following expression? function identity(arg: T): T { return arg; } const result = identity(42);

Key points

  • TypeScript infers the type of generics based on the argument passed
  • In this case, since 42 is a number, T is inferred as number
  • This results in the function returning a number type

Ready to go further?

Related questions