Community
QuestionTypeScript Deep Dive

When do generics actually pay off?

Lucas Moreau

Posted 5 days ago

I understand the syntax, but in my own code I never know when a generic is worth it versus a union or just `unknown`. Any rules of thumb?

18 replies
Accepted answer
Tomás VargaInstructor5 days ago

Use a generic when the output type depends on the input type. If a function returns the same thing whatever you pass in, you don't need one.

18 replies

Showing 4 of 18 · sorted by oldest

  1. Priya Raman5 days ago

    That one-liner is better than every blog post I read about it.

  2. Lucas MoreauAuthor4 days ago

    Refactored my fetch helper with this in mind — half the casts disappeared.

  3. Omar Haddad4 days ago

    Saving this thread.

Be kind and specific. Instructors read every thread.