#generics Clear
Tags #php #kotlin #bash #go #sql #rust #typescript #html #java #python #files #utils #strings #http #concurrency #async #json #arrays #security #types #crypto #database #dates #format
TypeScript Conditional Types Basics
`T extends U ? X : Y` lets types branch on shape. Combine with `infer` to extract pieces of complex types — the building block under `ReturnType`, `Parameters`, and most utility libraries.
TypeScript Type-Safe Event Emitter
A 30-line type-safe mini EventEmitter. The event-name → payload map is enforced at compile time, so emit and on calls can't drift apart.
Go Custom Generic Constraints
Type constraints are interfaces with type sets. Use them to express "any numeric" or "any ordered" without resorting to `any`.
Go Generic Map / Filter / Reduce
Three classic functional combinators, generically typed. Go's standard library doesn't ship these (yet), but they're short enough to drop into any project.
Go Generic Result / Option Type
Go doesn't have built-in Result types, but generics make them ergonomic. Combine with `errors.Is` and you have type-safe railroaded error handling.
TypeScript Generic Constraints with `extends`
Generics start unbounded — you can't access any properties of `T`. `T extends { … }` adds a constraint so the body can safely use known shape, while callers can still pass in narrower types.
Rust Generic Function with Trait Bounds
Use `T: Trait` to require a capability on a generic parameter. The `where` clause is the more readable variant when bounds get long.
Kotlin Variance — in vs out
`out T` (covariant) → producer of T; can be assigned to a wider type. `in T` (contravariant) → consumer of T; can be assigned to a narrower type. Without modifiers, generics are invariant (strict match).
Java Wildcards — extends vs super (PECS)
Producer-Extends, Consumer-Super. `? extends T` is read-only (you can take items OUT). `? super T` is write-only (you can put items IN). Lets generic APIs accept a wider range of types.
Go Generic Set Type
A type-safe Set built on `map[T]struct{}` and Go generics. Has Add / Has / Delete / Size — and uses zero bytes per entry for the value side.
Java Generic Class with Type Parameter
Stamp out type-safe containers and helpers — same class body, multiple element types. The `<T>` declaration is what makes it generic.
Java Generic Method + Bounded Type Parameters
A method can have its own type parameter independent of the class. `<T extends Comparable<T>>` constrains T to types that can be compared.
Python Result / Either with Generics
Encode "this might fail" in the type signature instead of throwing. Python 3.12+ generics syntax keeps it compact. Forces the caller to handle the failure branch.
Go Generic Function with Type Parameters
Go 1.18+ generics. `[T any]` is unconstrained; `[T comparable]` allows == and !=; custom constraints work too. The compiler infers T from arguments.
TypeScript Branded Types (nominal typing)
TypeScript is structurally typed, so `string` and `string` are interchangeable even when they semantically aren't (UserId vs PostId). The "brand" trick adds a phantom property that exists only at compile time, giving you nominal-ish typing.
Kotlin Star Projections (List<*>, etc.)
`List<*>` means "list of something I don't need to know exactly" — read-only with Any? as element type. Useful when you genuinely don't care about the parameter.
Kotlin reified Type Parameters
In an `inline` function, a `reified` type parameter survives erasure — you can use `T::class`, `is T`, `as T` at runtime. The killer use-case for JSON parsers ("give me a List<User>").
Kotlin Generic Class and Function
`<T>` declares a type parameter. Use it on classes (`Box<T>`) and functions (`fun <T> identity(x: T) = x`). Type bounds (`T : Comparable<T>`) constrain what T can be.