Skip to content
Open
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
7 changes: 1 addition & 6 deletions projects/s-js-utils/src/lib/callable-object.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { CallableObject } from "./callable-object";

interface Multiplier {
// tslint:disable:callable-types
(value: number): number;
}

class Multiplier extends CallableObject {
class Multiplier extends CallableObject<(value: number) => number> {
constructor(public factor: number) {
super((value: number) => value * this.factor);
}
Expand Down
20 changes: 11 additions & 9 deletions projects/s-js-utils/src/lib/callable-object.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/** @hidden */
export interface CallableObject<F extends (...args: any[]) => any> {
// tslint:disable-next-line:callable-types
(...args: Parameters<F>): ReturnType<F>;
}

/**
* Extend this for classes whose objects are directly callable. Sadly, you'll need to define an extra interface and repeat the typing information, for now. (Maybe fixable with TypeScript 3 tuples?)
* Extend this for classes whose objects are directly callable.
*
* ```ts
* interface Multiplier {
* (value: number): number;
* }
*
* class Multiplier extends CallableObject {
* class Multiplier extends CallableObject<(value: number) => number> {
* constructor(public factor: number) {
* super((value: number) => value * this.factor);
* }
Expand All @@ -18,8 +20,8 @@
* doubler(2); // 6
* ```
*/
export abstract class CallableObject {
constructor(f: Function) {
return Object.setPrototypeOf(f, new.target.prototype);
export abstract class CallableObject<F extends (...args: any[]) => any> {
constructor(callAction: F) {
return Object.setPrototypeOf(callAction, new.target.prototype);
}
}
9 changes: 2 additions & 7 deletions src/app/multiplier.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { CallableObject } from "../../projects/s-js-utils/src/lib/callable-object";
import { CallableObject } from "s-js-utils";

export interface Multiplier {
// tslint:disable:callable-types
(value: number): number;
}

export class Multiplier extends CallableObject {
export class Multiplier extends CallableObject<(value: number) => number> {
constructor(public factor: number) {
super((value: number) => value * this.factor);
}
Expand Down