Last active
September 3, 2021 14:56
-
-
Save intrnl/a7cc9a0864e130d9740e9b7baa2c3c08 to your computer and use it in GitHub Desktop.
Tree-shakeable class methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Module } from './module'; | |
export class Foo extends Module { | |
_foo = 'foo'; | |
} | |
export class Bar extends Module { | |
_bar = 'bar'; | |
} | |
export function fooOnly (foo: Foo, str: string) { | |
return foo._foo; | |
} | |
export function barOnly (bar: Bar, num: number) { | |
return bar._bar; | |
} | |
let foo = new Foo(); | |
foo.do(fooOnly, ''); // should pass | |
foo.do(barOnly, 0); // should fail | |
let bar = new Bar(); | |
bar.do(fooOnly, ''); // should fail | |
bar.do(barOnly, 0); // should pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type Extension<T> = (mod: T, ...args: any[]) => any; | |
export type ExtensionParameters<T extends (mod: any, ...args: any[]) => any> = | |
T extends (mod: any, ...args: infer P) => any ? P : never; | |
export abstract class Module { | |
do<T extends Extension<this>> (fn: T, ...args: ExtensionParameters<T>): ReturnType<T> { | |
return fn(this, ...args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Treeshake-able IndexedDB key value store