-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathts-function.ts
More file actions
32 lines (25 loc) · 874 Bytes
/
ts-function.ts
File metadata and controls
32 lines (25 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Creates functions that can be run either at compile time or run time.
export abstract class Function<I> {
declare input: I
declare output: ReturnType<this["x"]>
// @ts-ignore
abstract x(input: this["input"])
}
export function createFunction<F extends Function<any>>(
Fn: new () => F,
): TSFunction<F> {
const { x } = new Fn()
return ((input: F["input"]): F["output"] => x(input)) as any
}
declare const tsFunctionMarker: unique symbol
export type TSFunction<F extends Function<any>> = {
<T extends F["input"]>(
input: T,
): F["output"] & ReturnType<(F & { input: T })["x"]>
[tsFunctionMarker]: F
}
export type Call<
F extends TSFunction<Function<any>>,
I extends F[typeof tsFunctionMarker]["input"],
> = F[typeof tsFunctionMarker]["output"]
& ReturnType<(F[typeof tsFunctionMarker] & { input: I })["x"]>