Skip to content

Instantly share code, notes, and snippets.

@RedStone576
Created May 26, 2025 14:20
Show Gist options
  • Select an option

  • Save RedStone576/b5e4b0aa3a3c153ae40f2b0881c9bd4c to your computer and use it in GitHub Desktop.

Select an option

Save RedStone576/b5e4b0aa3a3c153ae40f2b0881c9bd4c to your computer and use it in GitHub Desktop.
Pipeline operator in typescript
class Pipe<T>
{
#value: T
constructor(value: T)
{
this.#value = value
}
get value(): T
{
return this.#value
}
$<U>(func: (arg: T) => U): Pipe<U>
{
const result = func(this.#value)
return new Pipe(result)
}
}
const { value: h } = new Pipe("balls")
.$(x => x.toUpperCase()) // string -> string
.$(x => x.length) // string -> number
.$(x => (console.log(x), x > 3)) // number -> boolean
// console log and return a value, arrow function implicitly returns the last expression
console.log(h) // boolean
/**/
new Pipe("test")
.value = "tes1"
// this was prevented dw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment