{"post":{"slug":"what-is-generic-programming-2026","title":"What is generic programming in 2026? Writing reusable code","excerpt":"Generic programming lets you write one function or class that works across many types safely, without losing type checking. Here is how it works.","category":"code","date":"July 22, 2026","author":"ByteLedger Team","content":"<span class=\"dropcap\">G</span>eneric programming means writing a function, class, or data structure that works across many types while the compiler still checks that the types make sense. Instead of writing separate versions of the same sorting function for numbers, strings, and custom objects, you write one generic version and let the specific type slot in at the call site. Using a loose catch-all type gives up that safety; copying and pasting one function per type gives up the reuse. Generics are the middle path.\n\n## What changed in 2026\n\n- **TypeScript inference for generics improved.** Contextual typing and its interaction with the `satisfies` operator reduce how often a type parameter needs to be spelled out manually for common cases.\n- **Go generics matured.** Added to the language in 2022, generics are now used throughout more of the standard library and popular packages, changing what idiomatic Go looks like day to day.\n- **AI-assisted refactoring suggests generics often.** Assistants frequently propose converting duplicated per-type functions into a single generic one, which is usually a real improvement, but the resulting constraint is worth checking rather than accepting a version typed with a loose catch-all just to make it compile.\n\n## A generic function, concretely\n\n```ts\nfunction firstElement<T>(items: T[]): T | undefined {\n  return items[0];\n}\n\nfirstElement([1, 2, 3]);       // T is inferred as number\nfirstElement([\"a\", \"b\"]);      // T is inferred as string\n```\n\n`T` is a placeholder, filled in per call. The function works for every type, and the return type still matches whatever went in, unlike a version typed loosely, which would return an untyped value and silently lose that information.\n\n## Generics vs a loose catch-all type\n\n| | Generic `<T>` | Loose catch-all (`any`) |\n| --- | --- | --- |\n| Type safety | Preserved end to end | Lost immediately |\n| Autocomplete on the result | Yes, matches input type | No |\n| Catches misuse at compile time | Yes | No |\n| Good for | Reusable, type-safe utilities | Genuinely unknown, unstructured data |\n\n## Constraints: narrowing what a generic accepts\n\n```ts\nfunction longest<T extends { length: number }>(a: T, b: T): T {\n  return a.length >= b.length ? a : b;\n}\n\nlongest(\"hello\", \"hi\");        // works: strings have length\nlongest([1, 2, 3], [1]);       // works: arrays have length\n```\n\nThe `extends` clause says `T` can be anything, as long as it has a `length` property. This is generics and duck typing working together: the constraint describes a shape, not a specific class. See [what duck typing is](/blog/what-is-duck-typing-2026) for the same idea without a compiler checking it.\n\n## Generics across languages\n\nTypeScript, Java, and C# all use angle-bracket syntax for type parameters, though each erases or reifies the type information differently at run time. Go added generics with square-bracket type parameters in version 1.18. Rust generics combine with trait bounds and are monomorphized at compile time, producing specialized code with no runtime cost. Syntax and capability vary enough between languages that it is worth checking current documentation for the one you are using rather than assuming they all behave identically.\n\n## When to reach for a generic, and when not to\n\nThe same logic needs to work identically across several types: use a generic. The types involved are genuinely unrelated in shape: a union type or overloads may be clearer. Tempted to use a loose catch-all type just to make the compiler stop complaining: that is almost always a sign a generic, or a narrower type, is the better fix. A one-off internal function used in exactly one place with one type: just write the concrete type; the generic adds nothing there.\n\n## FAQ\n\n**Are generics the same as templates in C++?**\nConceptually similar. Both let you write code parameterized by type, but the mechanics differ: C++ templates are compiled per instantiation, TypeScript generics are erased at compile time, and Rust monomorphizes similarly to C++.\n\n**Do generics slow down my code at run time?**\nIn TypeScript and Java, no. The extra type information is either erased or checked at compile time, with no runtime cost tied to using a generic instead of a concrete type.\n\n**What is a good first generic to write?**\nA small generic wrapper or a single-purpose utility like a generic first-element function. Small, focused examples build intuition faster than a generic class with several type parameters.\n\n**How is a generic different from duck typing?**\nGenerics are checked at compile time and require a statically typed language. Duck typing is typically a run-time concept. A constrained generic is close to compile-time duck typing.\n\n## Where to go next\n\n- [What is duck typing in 2026](/blog/what-is-duck-typing-2026)\n- [Interface vs abstract class in 2026](/blog/what-is-an-interface-vs-abstract-class-2026)\n- [The single responsibility principle explained in 2026](/blog/what-is-the-single-responsibility-principle-2026)","hue":200,"metaTitle":"What Is Generic Programming in 2026?","metaDescription":"What is generic programming in 2026? How generics let one function work across many types, with TypeScript examples, constraints, and common pitfalls.","keywords":["generic programming 2026","what are generics","generic programming typescript","generics vs any type","generic function example","type parameter programming","generics java example","generic programming vs duck typing","bounded generics constraint","why use generics"],"tldr":["**A generic lets a function or class work with any type while keeping type safety** — the actual type is filled in at the call site, not hardcoded.","**It is different from using any or object** — a generic remembers the specific type passed in; a loose catch-all type throws that information away.","**Constraints narrow what a generic can accept** — a bound like requiring a length property lets you call it while still working across many types.","**Most statically typed languages support them in some form** — TypeScript, Java, C#, Rust, Go, and Swift all have generics, with different syntax and capability.","**Skip:** reaching for a generic when a union type or a plain interface would be clearer; generics earn their complexity only when the relationship between input and output types actually varies."],"readTime":"4 min read","wordCount":790},"related":[{"slug":"what-is-an-argument-2026","title":"What Is an Argument in 2026? Plain Programming Guide","excerpt":"A clear explanation of what an argument is in programming in 2026, how it passes data into a function, and how it differs from a parameter.","category":"code","date":"May 13, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":212},{"slug":"what-is-duck-typing-2026","title":"What is duck typing in 2026? A plain-English guide","excerpt":"Duck typing means an object is defined by the methods it has, not by its declared type. Here is how it works in Python and JavaScript, and where it breaks down.","category":"code","date":"July 22, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":150},{"slug":"what-is-a-parameter-2026","title":"What is a parameter in 2026? The function input slot explained","excerpt":"A parameter is a named placeholder in a function definition that receives a value when the function is called. This guide explains parameters, how they differ from arguments, and the common types.","category":"code","date":"June 18, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":212}],"sameCategory":[{"slug":"bazel-build-explained-2026","title":"Bazel Explained: What Hermetic Builds Actually Cost","excerpt":"Bazel delivers reproducible, precisely cached builds and demands that you describe everything. Here is what you get, what it costs, and who should say no.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":145},{"slug":"graphql-federation-2026","title":"GraphQL Federation: One Graph, Many Teams","excerpt":"Federation composes independently-owned subgraphs into a single API. Here is what it solves, the operational cost it adds, and when a simpler gateway is enough.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":300},{"slug":"hermetic-builds-explained-2026","title":"Hermetic Builds Explained: Same Inputs, Same Output","excerpt":"A hermetic build depends only on declared inputs. Here is what that property gives you, the common leaks that break it, and how to move toward it incrementally.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":130},{"slug":"monorepo-tooling-compared-2026","title":"Monorepo Tooling Compared: What You Actually Need","excerpt":"Monorepo tools solve task orchestration and caching, not repository layout. Here is what each tier provides and how to tell when you have outgrown the simple option.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":150}],"newer":{"slug":"what-is-frame-generation-2026","title":"What Is Frame Generation in 2026: A Plain Explainer","excerpt":"Frame generation inserts AI-created frames between the ones your GPU actually renders, boosting the frame counter without fully boosting real performance. Here is what it does and when it is worth using.","category":"tech","date":"July 22, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":200},"older":{"slug":"what-is-hdmi-arc-2026","title":"What Is HDMI ARC? The 2026 Explainer","excerpt":"HDMI ARC lets a TV send audio back to a soundbar or receiver over the same cable that carries video. Here is what it does, its limits, and how it compares to eARC in 2026.","category":"tech","date":"July 22, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":280}}