Skip to content

Instantly share code, notes, and snippets.

View pfftdammitchris's full-sized avatar
💭
Dreaming

Christopher Tran pfftdammitchris

💭
Dreaming
View GitHub Profile
@pfftdammitchris
pfftdammitchris / snippet-11.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-11.ts
interface DatabaseConfig {
host: string;
port: number;
ssl: boolean;
poolSize: number;
}
const defaults: Partial<DatabaseConfig> = {
ssl: true,
poolSize: 10,
@pfftdammitchris
pfftdammitchris / snippet-12.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-12.ts
type RequiredFields = "url" | "method";
class RequestBuilder<T extends Partial<Record<RequiredFields, unknown>>> {
private data: T = {} as T;
url<U extends string>(value: U): RequestBuilder<T & { url: U }> {
this.data.url = value as any;
return this as any;
}
@pfftdammitchris
pfftdammitchris / snippet-13.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-13.ts
interface Endpoints {
"/users": { id: number; name: string }[];
"/users/:id": { id: number; name: string; email: string };
"/posts": { id: number; title: string; authorId: number }[];
}
async function get<P extends keyof Endpoints>(path: P): Promise<Endpoints[P]> {
const response = await fetch(`https://api.example.com${path}`);
return response.json();
}
@pfftdammitchris
pfftdammitchris / snippet-1.ts
Created July 18, 2026 05:06
TypeScript Variadic Tuple Types: Composing Function Signatures and Middleware Pipelines - snippet-1.ts
// Basic variadic tuple type
type Head<T extends any[]> = T extends [infer First, ...any[]] ? First : never;
type Tail<T extends any[]> = T extends [any, ...infer Rest] ? Rest : never;
// Usage
type Numbers = [1, 2, 3, 4];
type First = Head<Numbers>; // 1
type Rest = Tail<Numbers>; // [2, 3, 4]
@pfftdammitchris
pfftdammitchris / snippet-2.ts
Created July 18, 2026 05:06
TypeScript Variadic Tuple Types: Composing Function Signatures and Middleware Pipelines - snippet-2.ts
type Func<Args extends any[], Return> = (...args: Args) => Return;
type Pipe<Fns extends Func<any, any>[]> =
Fns extends [Func<infer A, infer B>, ...infer Rest]
? Rest extends Func<any, any>[]
? B extends Parameters<Rest[0]>[0]
? Pipe<[Func<A, ReturnType<Rest[0]>>, ...Tail<Rest>]>
: never
: Func<A, B>
: never;
@pfftdammitchris
pfftdammitchris / snippet-3.ts
Created July 18, 2026 05:06
TypeScript Variadic Tuple Types: Composing Function Signatures and Middleware Pipelines - snippet-3.ts
type PipeFn = (arg: any) => any;
type LastReturn<Fns extends PipeFn[]> =
Fns extends [...any[], infer Last]
? Last extends PipeFn
? ReturnType<Last>
: never
: never;
type ValidatePipe<Fns extends PipeFn[], Acc = []> =
@pfftdammitchris
pfftdammitchris / snippet-4.ts
Created July 18, 2026 05:06
TypeScript Variadic Tuple Types: Composing Function Signatures and Middleware Pipelines - snippet-4.ts
type Middleware<In, Out> = (context: In) => Out | Promise<Out>;
type PipelineResult<
Ctx,
Middlewares extends Middleware<any, any>[]
> = Middlewares extends [
Middleware<infer In, infer Out>,
...infer Rest
]
? Rest extends Middleware<any, any>[]
@pfftdammitchris
pfftdammitchris / snippet-5.ts
Created July 18, 2026 05:06
TypeScript Variadic Tuple Types: Composing Function Signatures and Middleware Pipelines - snippet-5.ts
type Curry<P extends any[], R> =
P extends [infer First, ...infer Rest]
? (arg: First) => Rest extends []
? R
: Curry<Rest, R>
: R;
function curry<P extends any[], R>(
fn: (...args: P) => R
): Curry<P, R> {
@pfftdammitchris
pfftdammitchris / snippet-6.ts
Last active July 18, 2026 05:06
TypeScript Variadic Tuple Types: Composing Function Signatures and Middleware Pipelines - snippet-6.ts
type ExpressRequest = { method: string; url: string };
type ExpressResponse = { send: (data: any) => void };
type NextFunction = () => void;
type Middleware<Req, Res> = (
req: Req,
res: Res,
next: NextFunction
) => void | Promise<void>;
@pfftdammitchris
pfftdammitchris / snippet-7.ts
Created July 18, 2026 05:06
TypeScript Variadic Tuple Types: Composing Function Signatures and Middleware Pipelines - snippet-7.ts
type Store<S> = {
getState: () => S;
dispatch: (action: any) => any;
};
type ReduxMiddleware<S, A> = (
store: Store<S>
) => (next: (action: A) => any) => (action: A) => any;
type ChainMiddleware<