|
1 | 1 | import { expectError, expectType } from 'tsd'; |
2 | 2 |
|
3 | | -import { propEq } from '../es'; |
| 3 | +import {__, propEq} from '../es'; |
4 | 4 |
|
5 | 5 | type Obj = { |
6 | 6 | union: 'foo' | 'bar'; |
7 | 7 | str: string; |
8 | | - num: number; |
| 8 | + int: number; |
| 9 | + numLike: number | `${number}`; |
| 10 | + optional?: string; |
| 11 | + nullable: string | null; |
9 | 12 | u: undefined; |
10 | 13 | n: null; |
11 | 14 | }; |
| 15 | +type NumArr = number[]; |
12 | 16 |
|
| 17 | +// ###################### |
13 | 18 | // propEq(val, name, obj) |
14 | 19 | expectType<boolean>(propEq('foo', 'union', {} as Obj)); |
15 | | -// non-union string fails |
16 | | -expectError(propEq('nope', 'union', {} as Obj)); |
17 | | -// completely different type fails |
18 | | -expectError(propEq(2, 'union', {} as Obj)); |
| 20 | +expectType<boolean>(propEq('1' as string, 'union', {} as Obj)); |
| 21 | +// union of number with literal types should work fine |
| 22 | +expectType<boolean>(propEq(1, 'numLike', {} as Obj)); |
| 23 | +expectType<boolean>(propEq('1', 'numLike', {} as Obj)); |
| 24 | +// optional types doesn't fire an error, if passed correct types |
| 25 | +expectType<boolean>(propEq('str', 'optional', {} as Obj)); |
| 26 | +expectType<boolean>(propEq(undefined, 'optional', {} as Obj)); |
| 27 | +// fires an error only on wrong type |
| 28 | +expectError(propEq(1, 'optional', {} as Obj)); |
| 29 | +expectError(propEq(null, 'optional', {} as Obj)); |
| 30 | +// nullable types doesn't fire an error, if passed correct types |
| 31 | +expectType<boolean>(propEq('str', 'nullable', {} as Obj)); |
| 32 | +expectType<boolean>(propEq(null, 'nullable', {} as Obj)); |
| 33 | +// fires an error only on wrong type |
| 34 | +expectError(propEq(1, 'nullable', {} as Obj)); |
| 35 | +expectError(propEq(undefined, 'nullable', {} as Obj)); |
| 36 | +// unknown field names fails |
| 37 | +expectError(propEq('foo', 'unknown', {} as Obj)); |
| 38 | +// should work with arrays as well |
| 39 | +expectType<boolean>(propEq(1, 0, [] as NumArr)); |
| 40 | +// numeric array should expect only numbers |
| 41 | +expectError(propEq('foo', 0, [] as NumArr)); |
| 42 | +// array can't accept string as prop name |
| 43 | +expectError(propEq(1, 'foo', [] as NumArr)); |
19 | 44 |
|
| 45 | +// ###################### |
20 | 46 | // propEq(val)(name)(obj) |
21 | 47 | expectType<boolean>(propEq('foo')('union')({} as Obj)); |
22 | | -// 'nope' is inferred as 'string' here. |
23 | | -expectType<boolean>(propEq('nope')('union')({} as Obj)); |
24 | | -// completely different type fails |
25 | | -expectError(propEq(2)('union')({} as Obj)); |
| 48 | +expectType<boolean>(propEq('nope' as string)('union')({} as Obj)); |
| 49 | +// since we use an exact literal type, it should fire an error |
| 50 | +expectError(propEq('nope')('union')({} as Obj)); |
| 51 | +// union of number with literal types should work fine |
| 52 | +expectType<boolean>(propEq(1)('numLike')({} as Obj)); |
| 53 | +expectType<boolean>(propEq('1')('numLike')({} as Obj)); |
| 54 | +// optional types doesn't fire an error, if passed correct types |
| 55 | +expectType<boolean>(propEq('str')('optional')({} as Obj)); |
| 56 | +expectType<boolean>(propEq(undefined)('optional')({} as Obj)); |
| 57 | +// fires an error only on wrong type |
| 58 | +expectError(propEq(1)('optional')({} as Obj)); |
| 59 | +expectError(propEq(null)('optional')({} as Obj)); |
| 60 | +// nullable types doesn't fire an error, if passed correct types |
| 61 | +expectType<boolean>(propEq('str')('nullable')({} as Obj)); |
| 62 | +expectType<boolean>(propEq(null)('nullable')({} as Obj)); |
| 63 | +// fires an error only on wrong type |
| 64 | +expectError(propEq(1)('nullable')({} as Obj)); |
| 65 | +expectError(propEq(undefined)('nullable')({} as Obj)); |
| 66 | +// unknown field names fails |
| 67 | +expectError(propEq('foo')('unknown')({} as Obj)); |
26 | 68 |
|
27 | | -// propEq(val)(name), obj) |
| 69 | +// ###################### |
| 70 | +// propEq(val)(name, obj) |
28 | 71 | expectType<boolean>(propEq('foo')('union', {} as Obj)); |
29 | | -// 'nope' is inferred as 'string' here. |
30 | | -expectType<boolean>(propEq('nope')('union', {} as Obj)); |
31 | | -// completely different type fails |
32 | | -expectError(propEq(2)('union', {} as Obj)); |
| 72 | +expectType<boolean>(propEq('nope' as string)('union', {} as Obj)); |
| 73 | +// since we use an exact literal type, it should fire an error |
| 74 | +expectError(propEq('nope')('union', {} as Obj)); |
| 75 | +// union of number with literal types should work fine |
| 76 | +expectType<boolean>(propEq(1)('numLike', {} as Obj)); |
| 77 | +expectType<boolean>(propEq('1')('numLike', {} as Obj)); |
| 78 | +// optional types doesn't fire an error, if passed correct types |
| 79 | +expectType<boolean>(propEq('str')('optional', {} as Obj)); |
| 80 | +expectType<boolean>(propEq(undefined)('optional', {} as Obj)); |
| 81 | +// fires an error only on wrong type |
| 82 | +expectError(propEq(1)('optional', {} as Obj)); |
| 83 | +expectError(propEq(null)('optional', {} as Obj)); |
| 84 | +// nullable types doesn't fire an error, if passed correct types |
| 85 | +expectType<boolean>(propEq('str')('nullable', {} as Obj)); |
| 86 | +expectType<boolean>(propEq(null)('nullable', {} as Obj)); |
| 87 | +// fires an error only on wrong type |
| 88 | +expectError(propEq(1)('nullable', {} as Obj)); |
| 89 | +expectError(propEq(undefined)('nullable', {} as Obj)); |
| 90 | +// unknown field names fails |
| 91 | +expectError(propEq('foo')('unknown', {} as Obj)); |
33 | 92 |
|
| 93 | +// ###################### |
34 | 94 | // propEq(val, name)(obj) |
35 | 95 | expectType<boolean>(propEq('foo', 'union')({} as Obj)); |
36 | | -// 'nope' is inferred as 'string' here. |
37 | | -expectType<boolean>(propEq('nope', 'union')({} as Obj)); |
38 | | -// completely different type fails |
39 | | -expectError(propEq(2, 'union')({} as Obj)); |
| 96 | +expectType<never>(propEq('nope' as string, 'union')({} as Obj)); |
| 97 | +// since we use an exact literal type, it should fire an error |
| 98 | +expectType<never>(propEq('nope', 'union')({} as Obj)); |
| 99 | +// union of number with literal types should work fine |
| 100 | +expectType<boolean>(propEq(1, 'numLike')({} as Obj)); |
| 101 | +expectType<boolean>(propEq('1', 'numLike')({} as Obj)); |
| 102 | +// optional types doesn't fire an error, if passed correct types |
| 103 | +expectType<boolean>(propEq('str', 'optional')({} as Obj)); |
| 104 | +expectType<boolean>(propEq(undefined, 'optional')({} as Obj)); |
| 105 | +// fires an error only on wrong type |
| 106 | +expectType<never>(propEq(1, 'optional')({} as Obj)); |
| 107 | +expectType<never>(propEq(null, 'optional')({} as Obj)); |
| 108 | +// nullable types doesn't fire an error, if passed correct types |
| 109 | +expectType<boolean>(propEq('str', 'nullable')({} as Obj)); |
| 110 | +expectType<boolean>(propEq(null, 'nullable')({} as Obj)); |
| 111 | +// fires an error only on wrong type |
| 112 | +expectType<never>(propEq(1, 'nullable')({} as Obj)); |
| 113 | +expectType<never>(propEq(undefined, 'nullable')({} as Obj)); |
| 114 | +// unknown field names fails |
| 115 | +expectError(propEq('foo', 'unknown')({} as Obj)); |
| 116 | + |
| 117 | +// ########################## |
| 118 | +// propEq(__, name, obj)(val) |
| 119 | +expectType<boolean>(propEq(__, 'union', {} as Obj)('foo')); |
| 120 | +// propEq(val, __, obj)(val) |
| 121 | +expectType<boolean>(propEq('foo', __, {} as Obj)('union')); |
| 122 | +// propEq(__, __, obj)(val, name) |
| 123 | +expectType<boolean>(propEq(__, __, {} as Obj)('foo', 'union')); |
| 124 | +// propEq(__, __, obj)(val)(name) |
| 125 | +expectType<boolean>(propEq(__, __, {} as Obj)('foo')('union')); |
| 126 | + |
| 127 | +expectError(propEq('foo', __, {} as Obj)('unknown')); |
| 128 | +expectError(propEq(__, __, {} as Obj)('foo', 'unknown')); |
| 129 | +expectError(propEq(__, __, {} as Obj)('foo')('unknown')); |
0 commit comments