|
| 1 | +/** |
| 2 | + * |
| 3 | + * NOTE: these examples are explicitly for testing types and may not be |
| 4 | + * suitable for actual runtime usage |
| 5 | + */ |
| 6 | +import { expectTypeOf } from 'expect-type'; |
| 7 | + |
| 8 | +// import { AsThunk, Class, ContextOf, ExpandThunkReturn, Named, Positional } from '[core-types]'; |
| 9 | +import { Resource } from '../resource'; |
| 10 | + |
| 11 | +/** |
| 12 | + * with no arguments specified |
| 13 | + */ |
| 14 | +class A extends Resource { |
| 15 | + a = 1; |
| 16 | +} |
| 17 | + |
| 18 | +// TODO: rename to create when there are no args? |
| 19 | +// are there use cases for class-based Resources without args? |
| 20 | +// no args seems like it'd be easier as a function-resource. |
| 21 | +// Valid, no args present |
| 22 | +// A.from(); |
| 23 | +A.from(() => ({})); |
| 24 | +A.from(() => []); |
| 25 | + |
| 26 | +// Invalid, A does not expect args |
| 27 | +// @ts-expect-error |
| 28 | +A.from(() => ({ positional: [1] })); |
| 29 | +// |
| 30 | +// Invalid, A does not expect args |
| 31 | +// @ts-expect-error |
| 32 | +A.from(() => ({ named: { foo: 2 } })); |
| 33 | + |
| 34 | +// valid, empty args are ok |
| 35 | +A.from(() => ({ positional: [], named: {} })); |
| 36 | + |
| 37 | +export class UsageA { |
| 38 | + a = A.from(this, () => ({})); |
| 39 | + a1 = A.from(this, () => []); |
| 40 | + |
| 41 | + // Invalid, A does not expect args |
| 42 | + // @ts-expect-error |
| 43 | + a2 = A.from(this, () => ({ positional: [1] })); |
| 44 | + |
| 45 | + // Invalid, A does not expect args |
| 46 | + // @ts-expect-error |
| 47 | + a3 = A.from(this, () => ({ named: { foo: 2 } })); |
| 48 | + |
| 49 | + // valid, empty args are ok |
| 50 | + a4 = A.from(this, () => ({ positional: [], named: {} })); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * with all arguments specified |
| 55 | + */ |
| 56 | +type BArgs = { |
| 57 | + positional: [num: number, greeting: string]; |
| 58 | + named: { |
| 59 | + num: number; |
| 60 | + str: string; |
| 61 | + }; |
| 62 | +}; |
| 63 | + |
| 64 | +export class B extends Resource<BArgs> { |
| 65 | + b = 'b'; |
| 66 | +} |
| 67 | + |
| 68 | +// Valid, all arguments provided |
| 69 | +B.from(() => { |
| 70 | + return { |
| 71 | + positional: [1, 'hi'], |
| 72 | + named: { num: 2, str: 'there' }, |
| 73 | + }; |
| 74 | +}); |
| 75 | + |
| 76 | +export class UsageB { |
| 77 | + // everything missing |
| 78 | + // @ts-expect-error |
| 79 | + b = B.from(this, () => ({})); |
| 80 | + |
| 81 | + // named is missing |
| 82 | + // @ts-expect-error |
| 83 | + b1 = B.from(this, () => ({ positional: [1, 'hi'] })); |
| 84 | + |
| 85 | + // positional is missing |
| 86 | + // @ts-expect-error |
| 87 | + b2 = B.from(this, () => ({ named: { num: 2, str: 'there' } })); |
| 88 | + |
| 89 | + // positional is incorrect |
| 90 | + // @ts-expect-error |
| 91 | + b3 = B.from(this, () => ({ positional: ['hi'] })); |
| 92 | + |
| 93 | + // named is incorrect |
| 94 | + // @ts-expect-error |
| 95 | + b4 = B.from(this, () => ({ named: { str: 'there' } })); |
| 96 | + |
| 97 | + // valid -- all args present |
| 98 | + b5 = B.from(this, () => ({ positional: [1, 'hi'], named: { num: 2, str: 'there' } })); |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * with all arguments, but capitalized (Signature style) |
| 103 | + */ |
| 104 | + |
| 105 | +type CArgs = { |
| 106 | + Positional: [number, string]; |
| 107 | + Named: { |
| 108 | + num: number; |
| 109 | + str: string; |
| 110 | + }; |
| 111 | +}; |
| 112 | + |
| 113 | +export class C extends Resource<CArgs> { |
| 114 | + c = 'c'; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * The return value of the thunk has the correct type |
| 119 | + */ |
| 120 | +export class UsageC { |
| 121 | + // decorator not needed for the type test (I don't want to import it) |
| 122 | + /* @use */ cUse = C.from(() => ({ positional: [1, 'two'], named: { num: 3, str: 'four' } })); |
| 123 | + cThis = C.from(this, () => ({ positional: [1, 'two'], named: { num: 3, str: 'four' } })); |
| 124 | +} |
| 125 | + |
| 126 | +expectTypeOf(new UsageC().cUse).toEqualTypeOf<C>(); |
| 127 | +expectTypeOf(new UsageC().cThis).toEqualTypeOf<C>(); |
0 commit comments