This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.d.ts
57 lines (48 loc) · 1.54 KB
/
index.d.ts
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
// Give "any" its own class
export class Any {
private _: true;
}
// Conditional returns can enforce identical types.
// See here: https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650
// prettier-ignore
type TestExact<Left, Right> =
(<U>() => U extends Left ? 1 : 0) extends (<U>() => U extends Right ? 1 : 0) ? Any : never;
type IsAny<T> = Any extends T ? ([T] extends [Any] ? 1 : 0) : 0;
export type Test<Left, Right> = IsAny<Left> extends 1
? IsAny<Right> extends 1
? 1
: "❌ Left type is 'any' but right type is not"
: IsAny<Right> extends 1
? "❌ Right type is 'any' but left type is not"
: [Left] extends [Right]
? [Right] extends [Left]
? Any extends TestExact<Left, Right>
? 1
: "❌ Unexpected or missing 'readonly' property"
: "❌ Right type is not assignable to left type"
: "❌ Left type is not assignable to right type";
type Assert<T, U> = U extends 1
? T // No error.
: IsAny<T> extends 1
? never // Ensure "any" is refused.
: U; // Return the error message.
/**
* Raise a compiler error when both argument types are not identical.
*/
export const assert: <Left, Right>(
left: Assert<Left, Test<Left, Right>>,
right: Assert<Right, Test<Left, Right>>
) => Right;
/**
* Placeholder value followed by "as T"
*/
export const _: any;
/** Same as "it" */
export const test: {
(fn: () => void): void;
(name?: string, fn?: () => void): void;
};
/** The "it" of spec.ts */
export const it: typeof test;
/** The "describe" of spec.ts */
export const describe: typeof test;