Skip to content
tezvyn:

Generics

50 bites tagged Generics — interview questions with model answers, and 60-second explainers.

TypeScript & Web APIs1 min read

Generic merge with an intersection return type

Use two type parameters T and U, return T & U, spread both objects; note later spread wins on key collisions and the type may not reflect that. Generics and intersection types.

iOS & Swift2 min read

Protocols with associated types and opaque types

A PAT leaves a placeholder type unresolved, so it has no single concrete shape and historically could not be used as a bare existential; opaque types with some let you return one fixed underlying conforming… generics and type erasure.

Go & Rust1 min read

Go interface-constraint generics versus Rust trait bounds

Go constrains type parameters with interfaces and may use dictionaries/shape stenciling; Rust uses trait bounds with full monomorphization for zero-cost specialization. understanding of generics design and monomorphization.

Go & Rust2 min read

Static dispatch over Write with generics in Rust

Write generically over the std::io::Write trait with a type parameter W: Write, letting the compiler monomorphize and inline per concrete type, avoiding the vtable indirection of Box<dyn Write>. static vs dynamic dispatch trade-offs.

Go & Rust1 min read

Associated types vs generic type parameters in traits

Associated types fix one type per implementer; generics allow many impls; Iterator::Item is the canonical example. trait design and type-level reasoning. claiming they are interchangeable or that generics are always better.

TypeScript & Web APIs2 min read

Create a CustomEvent with typed detail and a type-safe listener

Tests TypeScript generics with DOM events. Strong answer: generic CustomEvent wrapper, typed detail, listener typed via generic param. Red flag: using any for detail or casting event.target instead of parameterizing the event type.

TypeScript & Web APIs2 min read

Type a compose function using generics and overloads

It tests your ability to type higher-order pipelines where each function's output feeds the next input. Use function overloads with chained generics for each arity so TypeScript infers the composed parameter and return types.

TypeScript & Web APIs2 min read

Design a type-safe generic localStorage wrapper in TypeScript

Generic getItem<T> returns T|null via JSON.parse, setItem<T> stringifies, and a key-to-type map enforces safety. Preserving compile-time types across localStorage's string-only API.

TypeScript & Web APIs2 min read

Write a generic CreateSetters<T> that maps properties to setter methods

This tests TypeScript mapped types with key remapping and template literals. It iterates keyof T, remaps via as set plus Capitalize of string and K, and types values as setter methods. Red flag: manually typing setters or omitting string and K in Capitalize.

TypeScript & Web APIs2 min read

Implement recursive DeepReadonly<T> for nested objects and arrays

Tests recursive mapped types and conditional type narrowing. A strong answer uses a conditional to split arrays into ReadonlyArray, objects into readonly mapped types, and leaves primitives untouched.

TypeScript & Web APIs2 min read

Write a generic PickByValue<T, V> type

Tests conditional types and mapped-type key remapping. Great answer: K in keyof T as T[K] extends V ? K : never with value T[K]. Red flag: writing V extends T[K] instead, which reverses the assignability check and includes wrong keys.

TypeScript & Web APIs2 min read

Implement the built-in NonNullable<T> utility type from scratch

Tests conditional type distribution and union filtering. A strong answer uses distributive conditional types to map null and undefined to never while preserving other union members.

TypeScript & Web APIs2 min read

Implement MyParameters<T> using conditional types and infer

Tests if you can extract parameter types with infer. A strong answer matches T against a callable signature, uses infer to bind parameters into a tuple, and defaults to never for non-functions. Red flag: using any or indexing without inference.

TypeScript & Web APIs2 min read

Write a generic MakeOptional<T> mapped type without using Partial

This tests mapped type mechanics and the optionality modifier. A strong answer iterates over keyof T, adds the ? modifier, and preserves the original type via indexed access. A red flag is suggesting Object.assign or saying you would just use Partial.

TypeScript & Web APIs2 min read

Implement a generic PickByType<T, U> utility type

Tests mapped-type key filtering via conditional types and as remapping. Great answer: [K in keyof T as T[K] extends U ? K : never]: T[K]. Red flag: mapping all keys and setting values to never, which preserves keys instead of removing them.

TypeScript & Web APIs2 min read

Implement MyReturnType<T> using conditional types and infer

This tests type-level pattern matching with conditional types and infer. Answer uses T extends (...args: any[]) => infer R ? R : never, noting infer captures return slot, non-functions yield never. Red flag: conflating runtime values with compile-time types.

TypeScript & Web APIs2 min read

Write a generic fetchJSON<T> wrapper and explain its type safety benefits

Tests preserving type info across async boundaries via generics. Outline: write fetchJSON<T> returning Promise<T>, note response.json() is any, and show T lets callers lock in the response shape for compile-time checks.

TypeScript & Web APIs2 min read

Create a generic State class with getState and setState

Write class State<T> with private T, constructor(T), getState(): T, and setState(T). Binding a generic class parameter so methods share one consistent type. Using any instead of T, erasing type safety.

TypeScript & Web APIs2 min read

Write a generic type-safe getProperty using keyof

Command of TypeScript generics and keyof for compile-time property access. Declare generic T, accept key as keyof T, and return T[K]. Using any or string for the key, which erases type safety and allows invalid properties.

TypeScript & Web APIs2 min read

Explain generic constraints and provide a length-constrained generic example

Explain that extends requires properties; show T extends { length: number }; give a generic function signature. Your grasp of bounding generics to shapes without losing type safety. Using any or dropping extends.

TypeScript & Web APIs2 min read

Refactor a function using generics to accept any array type

It tests generic type parameters for preserving element types through a function boundary. Answer: declare T, accept T[], return T, and let the compiler infer the element type from the call site.

TypeScript & Web APIs2 min read

Create a generic fetchJson wrapper with typed response and error handling

Generic T parameter, optional RequestInit, throw if !res.ok, return res.json() as Promise<T>. marrying TypeScript generics to fetch for typed responses and runtime error handling. using any or omitting the ok check.

TypeScript & Web APIs2 min read

Write a generic TypeScript handler for mixed form inputs?

Tests TypeScript DOM narrowing. Strong answer: union-type ChangeEvent, narrow target with instanceof or tag checks, branch on element.type to read .checked for checkboxes or .value otherwise. Red flag: casting to any or assuming all inputs use .value.

TypeScript & Web APIs2 min read

How would you model fetchItem's return type with generics and conditional types?

Define base and extended Item, return T extends true ? ExtendedItem : BaseItem, and overload the plain boolean case. linking a generic boolean flag to a conditional return type.

Get Generics bites daily.

Five a day, five minutes, offline. With quizzes so it sticks.

Open testing — you’ll join as an early tester.