Skip to content

Add Pipeable.Class api #4669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .changeset/kind-poems-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
"effect": minor
---

Simplified the creation of pipeable classes.

```ts
class MyClass extends Pipeable.Class() {
constructor(public a: number) {
super()
}
methodA() {
return this.a
}
}
console.log(new MyClass(2).pipe((x) => x.methodA())) // 2
```

```ts
class A {
constructor(public a: number) {}
methodA() {
return this.a
}
}
class B extends Pipeable.Class(A) {
constructor(private b: string) {
super(b.length)
}
methodB() {
return [this.b, this.methodA()]
}
}
console.log(new B("pipe").pipe((x) => x.methodB())) // ['pipe', 4]
```
44 changes: 43 additions & 1 deletion packages/effect/src/Pipeable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
* @since 2.0.0
*/

import type { Ctor } from "./Types.js"

/**
* @since 2.0.0
* @category models
* @category Models
*/
export interface Pipeable {
pipe<A>(this: A): A
Expand Down Expand Up @@ -522,3 +524,43 @@ export const pipeArguments = <A>(self: A, args: IArguments): unknown => {
}
}
}

/**
* @since 3.15.0
* @category Models
*/
export interface PipeableConstructor {
new(...args: Array<any>): Pipeable
}

/**
* @since 3.15.0
* @category Prototypes
*/
export const Prototype: Pipeable = {
pipe() {
return pipeArguments(this, arguments)
}
}

const Base: PipeableConstructor = (function() {
function PipeableBase() {}
PipeableBase.prototype = Prototype
return PipeableBase as any
})()

/**
* @since 3.15.0
* @category Constructors
*/
export const Class: {
(): PipeableConstructor
<TBase extends Ctor>(klass: TBase): TBase & PipeableConstructor
} = (klass?: Ctor) =>
klass ?
class extends klass {
pipe() {
return pipeArguments(this, arguments)
}
}
: Base
5 changes: 5 additions & 0 deletions packages/effect/src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,8 @@ export type NotFunction<T> = T extends Function ? never : T
* @since 3.9.0
*/
export type NoExcessProperties<T, U> = T & { readonly [K in Exclude<keyof U, keyof T>]: never }

/**
* @since 3.15.0
*/
export type Ctor<T = {}> = new(...args: Array<any>) => T
42 changes: 40 additions & 2 deletions packages/effect/test/Pipeable.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it } from "@effect/vitest"
import { assertSome } from "@effect/vitest/utils"
import { Option } from "effect"
import { assertInstanceOf, assertSome, deepStrictEqual } from "@effect/vitest/utils"
import { Option, Pipeable } from "effect"

describe("Pipeable", () => {
it("pipeArguments", () => {
Expand Down Expand Up @@ -70,4 +70,42 @@ describe("Pipeable", () => {
126
)
})
it("pipeable", () => {
class A {
constructor(public a: number) {}
methodA() {
return this.a
}
}
class B extends Pipeable.Class(A) {
constructor(private b: string) {
super(b.length)
}
methodB() {
return [this.b, this.methodA()]
}
}
const b = new B("bb")

assertInstanceOf(b, A)
assertInstanceOf(b, B)
deepStrictEqual(b.methodB(), ["bb", 2])
deepStrictEqual(b.pipe((x) => x.methodB()), ["bb", 2])
})
it("Class", () => {
class A extends Pipeable.Class() {
constructor(public a: number) {
super()
}
methodA() {
return this.a
}
}
const a = new A(2)

assertInstanceOf(a, A)
assertInstanceOf(a, Pipeable.Class())
deepStrictEqual(a.methodA(), 2)
deepStrictEqual(a.pipe((x) => x.methodA()), 2)
})
})