-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.ts
More file actions
154 lines (122 loc) · 4.52 KB
/
Copy pathtest.ts
File metadata and controls
154 lines (122 loc) · 4.52 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Test ArgN types statically
import * as a from './index';
import { ArgsN, ArgsNum } from './pick-rangen';
import { ReplaceReturn } from './replace-return';
import { ArgI } from './pick';
import { CtorArgs } from './ctor-args';
function staticAssert1Args() {
function foo(a: string, b: number) { /** */ }
const assertion: a.Arg2<typeof foo> = 123;
function boo(a: a.Arg1<typeof foo>, b: a.Arg2<typeof foo>) { /**/ }
boo('hello', assertion);
}
function staticAssert2ArgN() {
function f(
a: string,
b: number,
c: boolean,
d: undefined,
e: object,
f: Function,
g: string,
h: number,
i: boolean,
j: null,
) { /** */ }
const arg1: a.Arg1<typeof f> = 'hello';
const arg2: a.Arg2<typeof f> = 123;
const arg3: a.Arg3<typeof f> = true;
const arg4: a.Arg4<typeof f> = undefined;
const arg5: a.Arg5<typeof f> = {};
const arg6: a.Arg6<typeof f> = () => {};
const arg7: a.Arg7<typeof f> = 'hello';
const arg8: a.Arg8<typeof f> = 321;
const arg9: a.Arg9<typeof f> = false;
const arg10: a.Arg10<typeof f> = null;
}
function staticAssert3Pre() {
function foo(a: string, b: number) {}
let boo: (x: boolean, a: string, b: number) => void;
boo = foo as any as a.Pre1Arg2<boolean, typeof foo>;
let goo: (x: boolean, y: number, a: string, b: number) => void;
goo = foo as any as a.Pre2Arg2<boolean, number, typeof foo>;
}
function staticAssert4PreN() {
function foo(a: string, b: number) {}
let boo: (x: boolean, a: string, b: number) => void;
boo = foo as any as a.Pre1ArgN<boolean, typeof foo>;
let goo: (x: boolean, y: number, a: string, b: number) => void;
goo = foo as any as a.Pre2ArgN<boolean, number, typeof foo>;
}
function staticAssert5PickRange() {
function foo(a: string, b: number, c: boolean) {}
const args: a.Args2off1<typeof foo> = [ 123, true ];
}
function staticAssert6Post() {
function foo(a: string, b: number) {}
let boo: (a: string, b: number, x: boolean) => void;
boo = foo as any as a.Post1Arg2<boolean, typeof foo>;
let goo: (a: string, b: number, x: boolean, y: number) => void;
goo = foo as any as a.Post2Arg2<boolean, number, typeof foo>;
}
function staticAssert7PickRangeN() {
function foo(a: string, b: number, c: boolean) {}
const args: ArgsN<typeof foo> = [ 'hello', 123, true ];
function boo() {}
const args2: ArgsN<typeof boo> = [];
}
function staticAssert8PostN() {
function foo(a: string, b: number) {}
let boo: (a: string, b: number, x: boolean) => void;
boo = foo as any as a.Post1ArgN<boolean, typeof foo>;
let goo: (a: string, b: number, x: boolean, y: number) => void;
goo = foo as any as a.Post2ArgN<boolean, number, typeof foo>;
}
function staticAssert9ReplaceReturn() {
function foo(a: string, b: number) {
return 'hello world';
}
let boo: (a: string, b: number, x: boolean) => number;
boo = foo as any as ReplaceReturn<number, a.Post1ArgN<boolean, typeof foo>>;
}
function staticAssert10ReplaceReturn() {
function foo(a: number, b: string): number { return 0; }
function boo(a: number, b: string): string { return '0'; }
const booFromFoo: ReplaceReturn<string, typeof foo> = boo;
}
function staticAssertRestArgsN() {
const myCallbacks = {
foo: (a: number, b: number) => a + b,
boo: (a: number) => a + 10,
};
function call<
CallbackName extends keyof Callbacks,
Callbacks extends { [k: string]: (...args: any[]) => any } = typeof myCallbacks,
Callback extends (...args: any[]) => any = Callbacks[CallbackName],
>(callbackName: CallbackName, ...args: ArgsN<Callback>): ReturnType<Callback> {
return (myCallbacks as { [k: string]: Function })[callbackName as any](...args);
}
call('foo', 1, 2);
}
function staticAssertArgsNum() {
function foo(a: number, b: string): number { return 0; }
function boo(a: number, b: string, c: boolean): string { return '0'; }
const fooI: ArgsNum<typeof foo> = 2;
const booI: ArgsNum<typeof boo> = 3;
}
function staticAssertArgI() {
function boo(a: number, b: string, c: boolean): string { return '0'; }
const a: ArgI<typeof boo, 0> = 123;
const b: ArgI<typeof boo, 1> = '123';
const c: ArgI<typeof boo, 2> = false;
}
function ctorArgs() {
class A {
constructor(
public x: number,
public y: string,
public z: boolean,
) {}
}
const _: CtorArgs<typeof A> = [ 0, '0', false ];
}