|
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 | +// propEq doesn't create unnecessary values constraint for object |
| 21 | +expectType<boolean>(propEq('1', 'union', {} as Obj)); |
| 22 | +// union of number with literal types should work fine |
| 23 | +expectType<boolean>(propEq(1, 'numLike', {} as Obj)); |
| 24 | +expectType<boolean>(propEq('1', 'numLike', {} as Obj)); |
| 25 | +// optional types doesn't fire an error, if passed correct types |
| 26 | +expectType<boolean>(propEq('str', 'optional', {} as Obj)); |
| 27 | +expectType<boolean>(propEq(undefined, 'optional', {} as Obj)); |
| 28 | +// fires an error only on wrong type |
| 29 | +expectError(propEq(1, 'optional', {} as Obj)); |
| 30 | +expectError(propEq(null, 'optional', {} as Obj)); |
| 31 | +// nullable types doesn't fire an error, if passed correct types |
| 32 | +expectType<boolean>(propEq('str', 'nullable', {} as Obj)); |
| 33 | +expectType<boolean>(propEq(null, 'nullable', {} as Obj)); |
| 34 | +// fires an error only on wrong type |
| 35 | +expectError(propEq(1, 'nullable', {} as Obj)); |
| 36 | +expectError(propEq(undefined, 'nullable', {} as Obj)); |
| 37 | +// unknown field names fails |
| 38 | +expectError(propEq('foo', 'unknown', {} as Obj)); |
| 39 | +// should work with arrays as well |
| 40 | +expectType<boolean>(propEq(1, 0, [] as NumArr)); |
| 41 | +// numeric array should expect only numbers |
| 42 | +expectError(propEq('foo', 0, [] as NumArr)); |
| 43 | +// array can't accept string as prop name |
| 44 | +expectError(propEq(1, 'foo', [] as NumArr)); |
19 | 45 |
|
| 46 | +// ###################### |
20 | 47 | // propEq(val)(name)(obj) |
21 | 48 | expectType<boolean>(propEq('foo')('union')({} as Obj)); |
22 | 49 | // 'nope' is inferred as 'string' here. |
23 | 50 | expectType<boolean>(propEq('nope')('union')({} as Obj)); |
24 | | -// completely different type fails |
25 | | -expectError(propEq(2)('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 | 72 | // 'nope' is inferred as 'string' here. |
30 | 73 | expectType<boolean>(propEq('nope')('union', {} as Obj)); |
31 | | -// completely different type fails |
32 | | -expectError(propEq(2)('union', {} as Obj)); |
| 74 | +// union of number with literal types should work fine |
| 75 | +expectType<boolean>(propEq(1)('numLike', {} as Obj)); |
| 76 | +expectType<boolean>(propEq('1')('numLike', {} as Obj)); |
| 77 | +// optional types doesn't fire an error, if passed correct types |
| 78 | +expectType<boolean>(propEq('str')('optional', {} as Obj)); |
| 79 | +expectType<boolean>(propEq(undefined)('optional', {} as Obj)); |
| 80 | +// fires an error only on wrong type |
| 81 | +expectError(propEq(1)('optional', {} as Obj)); |
| 82 | +expectError(propEq(null)('optional', {} as Obj)); |
| 83 | +// nullable types doesn't fire an error, if passed correct types |
| 84 | +expectType<boolean>(propEq('str')('nullable', {} as Obj)); |
| 85 | +expectType<boolean>(propEq(null)('nullable', {} as Obj)); |
| 86 | +// fires an error only on wrong type |
| 87 | +expectError(propEq(1)('nullable', {} as Obj)); |
| 88 | +expectError(propEq(undefined)('nullable', {} as Obj)); |
| 89 | +// unknown field names fails |
| 90 | +expectError(propEq('foo')('unknown', {} as Obj)); |
33 | 91 |
|
| 92 | +// ###################### |
34 | 93 | // propEq(val, name)(obj) |
35 | 94 | expectType<boolean>(propEq('foo', 'union')({} as Obj)); |
36 | 95 | // 'nope' is inferred as 'string' here. |
37 | 96 | expectType<boolean>(propEq('nope', 'union')({} as Obj)); |
38 | | -// completely different type fails |
39 | | -expectError(propEq(2, 'union')({} as Obj)); |
| 97 | +// union of number with literal types should work fine |
| 98 | +expectType<boolean>(propEq(1, 'numLike')({} as Obj)); |
| 99 | +expectType<boolean>(propEq('1', 'numLike')({} as Obj)); |
| 100 | +// optional types doesn't fire an error, if passed correct types |
| 101 | +expectType<boolean>(propEq('str', 'optional')({} as Obj)); |
| 102 | +expectType<boolean>(propEq(undefined, 'optional')({} as Obj)); |
| 103 | +// fires an error only on wrong type |
| 104 | +expectError(propEq(1, 'optional')({} as Obj)); |
| 105 | +expectError(propEq(null, 'optional')({} as Obj)); |
| 106 | +// nullable types doesn't fire an error, if passed correct types |
| 107 | +expectType<boolean>(propEq('str', 'nullable')({} as Obj)); |
| 108 | +expectType<boolean>(propEq(null, 'nullable')({} as Obj)); |
| 109 | +// fires an error only on wrong type |
| 110 | +expectError(propEq(1, 'nullable')({} as Obj)); |
| 111 | +expectError(propEq(undefined, 'nullable')({} as Obj)); |
| 112 | +// unknown field names fails |
| 113 | +expectError(propEq('foo', 'unknown')({} as Obj)); |
| 114 | + |
| 115 | +// ########################## |
| 116 | +// propEq(__, name, obj)(val) |
| 117 | +expectType<boolean>(propEq(__, 'union', {} as Obj)('foo')); |
| 118 | +// propEq(val, __, obj)(val) |
| 119 | +expectType<boolean>(propEq('foo', __, {} as Obj)('union')); |
| 120 | +// propEq(__, __, obj)(val, name) |
| 121 | +expectType<boolean>(propEq(__, __, {} as Obj)('foo', 'union')); |
| 122 | +// propEq(__, __, obj)(val)(name) |
| 123 | +expectType<boolean>(propEq(__, __, {} as Obj)('foo')('union')); |
| 124 | + |
| 125 | +expectError(propEq('foo', __, {} as Obj)('unknown')); |
| 126 | +expectError(propEq(__, __, {} as Obj)('foo', 'unknown')); |
| 127 | +expectError(propEq(__, __, {} as Obj)('foo')('unknown')); |
0 commit comments