QamoosTech
Programming FundamentalsTypes and typingIntermediateTypeScriptPython

Generics

Pronunciationjuh-NAIR-iks

Definition

Generics let one function or class work with many types while keeping them consistent, for example a list of numbers or a list of strings.

Where you hear it

In TypeScript, Java and typed Python code, library documentation, and API design discussions.

Examples

  • A generic list keeps track of what kind of items it holds.
  • The function is generic: it returns the same type it was given.
  • A generic cache keeps the type of the values it stores, so callers get the right type back.

Common mistake

Making everything generic. Use generics only when the same logic truly applies to many types.

Don't confuse with

The any type, which turns checking off. A generic keeps the type information.

Say it at work

  • In a meeting or chatLet's make this container generic so it can hold any item type.
  • In an email or ticketThe return type is generic, so the editor knows exactly what comes back.