1 | import type { IsAny, IsUnknown } from '../../src/tsHelpers'
|
2 |
|
3 | export function expectType<T>(t: T): T {
|
4 | return t
|
5 | }
|
6 |
|
7 | type Equals<T, U> = IsAny<
|
8 | T,
|
9 | never,
|
10 | IsAny<U, never, [T] extends [U] ? ([U] extends [T] ? any : never) : never>
|
11 | >
|
12 | export function expectExactType<T>(t: T) {
|
13 | return <U extends Equals<T, U>>(u: U) => {}
|
14 | }
|
15 |
|
16 | type EnsureUnknown<T extends any> = IsUnknown<T, any, never>
|
17 | export function expectUnknown<T extends EnsureUnknown<T>>(t: T) {
|
18 | return t
|
19 | }
|
20 |
|
21 | type EnsureAny<T extends any> = IsAny<T, any, never>
|
22 | export function expectExactAny<T extends EnsureAny<T>>(t: T) {
|
23 | return t
|
24 | }
|
25 |
|
26 | type IsNotAny<T> = IsAny<T, never, any>
|
27 | export function expectNotAny<T extends IsNotAny<T>>(t: T): T {
|
28 | return t
|
29 | }
|
30 |
|
31 | expectType<string>('5' as string)
|
32 | expectType<string>('5' as const)
|
33 | expectType<string>('5' as any)
|
34 | expectExactType('5' as const)('5' as const)
|
35 |
|
36 | expectExactType('5' as string)('5' as const)
|
37 |
|
38 | expectExactType('5' as any)('5' as const)
|
39 | expectUnknown('5' as unknown)
|
40 |
|
41 | expectUnknown('5' as const)
|
42 |
|
43 | expectUnknown('5' as any)
|
44 | expectExactAny('5' as any)
|
45 |
|
46 | expectExactAny('5' as const)
|
47 |
|
48 | expectExactAny('5' as unknown)
|