Skip to content

Commit

Permalink
asdf
Browse files Browse the repository at this point in the history
  • Loading branch information
gnlow committed May 21, 2023
1 parent 548929b commit 6868523
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
23 changes: 17 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
type Ufcs<C extends {"prototype": any}> = {
[K in keyof C["prototype"]]:
(_this: C) => C["prototype"][K]
} & { new (): C }
C["prototype"][K] extends (...args: any[]) => any
? (...args: Parameters<C["prototype"][K]>) => (_this: C) => ReturnType<C["prototype"][K]>
: never
} & C

const ufcs = <C extends {"prototype": any}>(_class: C) => {
export const ufcs = <C extends {"prototype": unknown}>(_class: C) => {
Object.getOwnPropertyNames(_class.prototype)
.forEach(prop => {
if (prop != "prototype") {
if (prop != "constructor") {
Object.defineProperty(_class, prop, {
value: (_this: any) => _this[prop]()
value: (...args: unknown[]) => (_this: any) => _this[prop](...args)
})
}
})
return _class as unknown as Ufcs<C>
}
}

const Duck = ufcs(class Duck {
quack(num: number) {
console.log("Quack!", num)
}
})
const duck = new Duck
Duck.quack(123)(duck); // Quack! 123
duck.quack(123) // Quack! 123
8 changes: 8 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ufcs } from "./mod.ts"

const Duck = ufcs(class Duck {
quack() {
console.log("Quack!")
}
})
Duck.quack(new Duck) // Quack!

0 comments on commit 6868523

Please sign in to comment.