Skip to content

Instantly share code, notes, and snippets.

@mlaopane
Created October 31, 2025 09:40
Show Gist options
  • Select an option

  • Save mlaopane/3078c13e166c5ace5cc28d0b433b3c74 to your computer and use it in GitHub Desktop.

Select an option

Save mlaopane/3078c13e166c5ace5cc28d0b433b3c74 to your computer and use it in GitHub Desktop.
Pipe functions with type inference on each pipe call
type Transformer<I, O> = (input: I) => O
/**
* @example
* ```ts
* Pipeline.init({id: 1, name: "John"})
* .pipe((user) => `${user.name}-${user.id}`)
* .pipe((slug) => slug.toLocaleLowerCase())
* .value()
* // Result === "john-1"
* ```
*/
export class Pipeline<I> {
private readonly currentValue: I
private constructor(initialValue: I) {
this.currentValue = initialValue
}
static init<T>(initialValue: T): Pipeline<T> {
return new Pipeline(initialValue)
}
pipe<O>(transformer: Transformer<I, O>): Pipeline<O> {
const next = transformer(this.currentValue)
return new Pipeline<O>(next)
}
value(): I {
return this.currentValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment