|
| 1 | +import { Flow } from '../../src/index.js'; |
| 2 | +import { describe, it, expectTypeOf } from 'vitest'; |
| 3 | + |
| 4 | +describe('map step return type inference bug', () => { |
| 5 | + it('should preserve specific return type from map handler, not collapse to any[]', () => { |
| 6 | + const flow = new Flow<{ items: string[] }>({ slug: 'test' }) |
| 7 | + .array({ slug: 'chunks' }, async ({ run }) => { |
| 8 | + return [{ data: 'chunk1' }, { data: 'chunk2' }]; |
| 9 | + }) |
| 10 | + .map( |
| 11 | + { slug: 'processChunks', array: 'chunks' }, |
| 12 | + async (chunk) => { |
| 13 | + return { |
| 14 | + chunkIndex: 0, |
| 15 | + successes: ['success1'], |
| 16 | + errors: [{ line: 1, error: 'test error' }], // Non-empty array for inference |
| 17 | + }; |
| 18 | + } |
| 19 | + ) |
| 20 | + .step( |
| 21 | + { slug: 'aggregate', dependsOn: ['processChunks'] }, |
| 22 | + async ({ run, processChunks }) => { |
| 23 | + // Verify types are inferred correctly |
| 24 | + expectTypeOf(processChunks).not.toEqualTypeOf<any[]>(); |
| 25 | + |
| 26 | + // These should all have proper types, not any |
| 27 | + for (const result of processChunks) { |
| 28 | + expectTypeOf(result.chunkIndex).toEqualTypeOf<number>(); |
| 29 | + expectTypeOf(result.chunkIndex).not.toEqualTypeOf<any>(); |
| 30 | + expectTypeOf(result.successes).toEqualTypeOf<string[]>(); |
| 31 | + expectTypeOf(result.successes).not.toEqualTypeOf<any>(); |
| 32 | + expectTypeOf(result.errors).toMatchTypeOf<Array<{ line: number; error: string }>>(); |
| 33 | + expectTypeOf(result.errors).not.toEqualTypeOf<any>(); |
| 34 | + } |
| 35 | + |
| 36 | + return { done: true }; |
| 37 | + } |
| 38 | + ); |
| 39 | + |
| 40 | + // Verify the map step output type is not any[] |
| 41 | + type ProcessChunksOutput = typeof flow extends Flow<any, any, infer Steps, any> |
| 42 | + ? Steps['processChunks'] |
| 43 | + : never; |
| 44 | + |
| 45 | + expectTypeOf<ProcessChunksOutput>().not.toEqualTypeOf<any[]>(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should preserve complex nested types through map', () => { |
| 49 | + // Note: optional properties not in the return object are not inferred by TypeScript |
| 50 | + type ComplexResult = { |
| 51 | + nested: { deep: { value: string } }; |
| 52 | + array: number[]; |
| 53 | + }; |
| 54 | + |
| 55 | + const flow = new Flow<Record<string, never>>({ slug: 'test' }) |
| 56 | + .array({ slug: 'items' }, () => [1, 2, 3]) |
| 57 | + .map({ slug: 'transform', array: 'items' }, async (item) => { |
| 58 | + return { |
| 59 | + nested: { deep: { value: 'test' } }, |
| 60 | + array: [1, 2, 3] |
| 61 | + }; |
| 62 | + }) |
| 63 | + .step({ slug: 'use', dependsOn: ['transform'] }, ({ transform }) => { |
| 64 | + expectTypeOf(transform).toEqualTypeOf<ComplexResult[]>(); |
| 65 | + expectTypeOf(transform).not.toEqualTypeOf<any[]>(); |
| 66 | + |
| 67 | + // Verify nested structure is preserved |
| 68 | + expectTypeOf(transform[0].nested.deep.value).toEqualTypeOf<string>(); |
| 69 | + expectTypeOf(transform[0].nested.deep.value).not.toEqualTypeOf<any>(); |
| 70 | + expectTypeOf(transform[0].array).toEqualTypeOf<number[]>(); |
| 71 | + expectTypeOf(transform[0].array).not.toEqualTypeOf<any>(); |
| 72 | + |
| 73 | + return { ok: true }; |
| 74 | + }); |
| 75 | + |
| 76 | + type TransformOutput = typeof flow extends Flow<any, any, infer Steps, any> |
| 77 | + ? Steps['transform'] |
| 78 | + : never; |
| 79 | + |
| 80 | + expectTypeOf<TransformOutput>().toEqualTypeOf<ComplexResult[]>(); |
| 81 | + expectTypeOf<TransformOutput>().not.toEqualTypeOf<any[]>(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should preserve union-like return types from map', () => { |
| 85 | + // Test that return types with discriminated union pattern are inferred correctly |
| 86 | + const flow = new Flow<number[]>({ slug: 'test' }) |
| 87 | + .map({ slug: 'process' }, async (item) => { |
| 88 | + // Return explicit objects to help TypeScript inference |
| 89 | + const success = { success: true as const, data: 'ok' }; |
| 90 | + const failure = { success: false as const, error: 'fail' }; |
| 91 | + return Math.random() > 0.5 ? success : failure; |
| 92 | + }) |
| 93 | + .step({ slug: 'aggregate', dependsOn: ['process'] }, ({ process }) => { |
| 94 | + expectTypeOf(process).not.toEqualTypeOf<any[]>(); |
| 95 | + |
| 96 | + // Verify the inferred type preserves the shape |
| 97 | + const firstResult = process[0]; |
| 98 | + expectTypeOf(firstResult.success).toEqualTypeOf<boolean>(); |
| 99 | + |
| 100 | + return { done: true }; |
| 101 | + }); |
| 102 | + |
| 103 | + type ProcessOutput = typeof flow extends Flow<any, any, infer Steps, any> |
| 104 | + ? Steps['process'] |
| 105 | + : never; |
| 106 | + |
| 107 | + expectTypeOf<ProcessOutput>().not.toEqualTypeOf<any[]>(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should work with inferred return types (no explicit Promise type)', () => { |
| 111 | + const flow = new Flow<string[]>({ slug: 'test' }) |
| 112 | + .map({ slug: 'transform' }, (item) => { |
| 113 | + return { value: item.toUpperCase(), length: item.length }; |
| 114 | + }) |
| 115 | + .step({ slug: 'use', dependsOn: ['transform'] }, ({ transform }) => { |
| 116 | + // Should infer { value: string; length: number }[] |
| 117 | + expectTypeOf(transform).toEqualTypeOf<{ value: string; length: number }[]>(); |
| 118 | + expectTypeOf(transform).not.toEqualTypeOf<any[]>(); |
| 119 | + |
| 120 | + for (const result of transform) { |
| 121 | + expectTypeOf(result.value).toEqualTypeOf<string>(); |
| 122 | + expectTypeOf(result.value).not.toEqualTypeOf<any>(); |
| 123 | + expectTypeOf(result.length).toEqualTypeOf<number>(); |
| 124 | + expectTypeOf(result.length).not.toEqualTypeOf<any>(); |
| 125 | + } |
| 126 | + |
| 127 | + return { ok: true }; |
| 128 | + }); |
| 129 | + |
| 130 | + type TransformOutput = typeof flow extends Flow<any, any, infer Steps, any> |
| 131 | + ? Steps['transform'] |
| 132 | + : never; |
| 133 | + |
| 134 | + expectTypeOf<TransformOutput>().toEqualTypeOf<{ value: string; length: number }[]>(); |
| 135 | + expectTypeOf<TransformOutput>().not.toEqualTypeOf<any[]>(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should work with root map (no array dependency)', () => { |
| 139 | + const flow = new Flow<string[]>({ slug: 'test' }) |
| 140 | + .map({ slug: 'uppercase' }, (item) => { |
| 141 | + return { original: item, transformed: item.toUpperCase() }; |
| 142 | + }) |
| 143 | + .step({ slug: 'aggregate', dependsOn: ['uppercase'] }, ({ uppercase }) => { |
| 144 | + expectTypeOf(uppercase).toEqualTypeOf<{ original: string; transformed: string }[]>(); |
| 145 | + expectTypeOf(uppercase).not.toEqualTypeOf<any[]>(); |
| 146 | + |
| 147 | + for (const result of uppercase) { |
| 148 | + expectTypeOf(result.original).toEqualTypeOf<string>(); |
| 149 | + expectTypeOf(result.original).not.toEqualTypeOf<any>(); |
| 150 | + expectTypeOf(result.transformed).toEqualTypeOf<string>(); |
| 151 | + expectTypeOf(result.transformed).not.toEqualTypeOf<any>(); |
| 152 | + } |
| 153 | + |
| 154 | + return { count: uppercase.length }; |
| 155 | + }); |
| 156 | + |
| 157 | + type UppercaseOutput = typeof flow extends Flow<any, any, infer Steps, any> |
| 158 | + ? Steps['uppercase'] |
| 159 | + : never; |
| 160 | + |
| 161 | + expectTypeOf<UppercaseOutput>().toEqualTypeOf<{ original: string; transformed: string }[]>(); |
| 162 | + expectTypeOf<UppercaseOutput>().not.toEqualTypeOf<any[]>(); |
| 163 | + }); |
| 164 | +}); |
0 commit comments