@zeix/cause-effect 0.15.1 → 0.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,6 @@ import {
10
10
  type Store,
11
11
  state,
12
12
  store,
13
- toMutableSignal,
14
13
  toSignal,
15
14
  type UnknownRecord,
16
15
  } from '..'
@@ -20,31 +19,28 @@ import {
20
19
  describe('toSignal', () => {
21
20
  describe('type inference and runtime behavior', () => {
22
21
  test('converts array to Store<Record<string, T>>', () => {
23
- const arr = [
22
+ const result = toSignal([
24
23
  { id: 1, name: 'Alice' },
25
24
  { id: 2, name: 'Bob' },
26
- ]
27
- const result = toSignal(arr)
25
+ ])
28
26
 
29
27
  // Runtime behavior
30
28
  expect(isStore(result)).toBe(true)
31
29
  expect(result['0'].get()).toEqual({ id: 1, name: 'Alice' })
32
30
  expect(result['1'].get()).toEqual({ id: 2, name: 'Bob' })
33
31
 
34
- // Type inference test - now correctly returns Store<Record<string, {id: number, name: string}>>
35
- const typedResult: Store<
36
- Record<string, { id: number; name: string }>
37
- > = result
32
+ // Type inference test - now correctly returns Store<Record<number, {id: number, name: string}>>
33
+ const typedResult: Store<{ id: number; name: string }[]> = result
38
34
  expect(typedResult).toBeDefined()
39
35
  })
40
36
 
41
37
  test('converts empty array to Store<Record<string, never>>', () => {
42
- const arr: never[] = []
43
- const result = toSignal(arr)
38
+ const result = toSignal([])
44
39
 
45
40
  // Runtime behavior
46
41
  expect(isStore(result)).toBe(true)
47
- expect(Object.keys(result).length).toBe(0)
42
+ expect(result.length).toBe(0)
43
+ expect(Object.keys(result).length).toBe(1) // length property
48
44
  })
49
45
 
50
46
  test('converts record to Store<T>', () => {
@@ -145,11 +141,10 @@ describe('toSignal', () => {
145
141
 
146
142
  describe('edge cases', () => {
147
143
  test('handles nested arrays', () => {
148
- const nestedArr = [
144
+ const result = toSignal([
149
145
  [1, 2],
150
146
  [3, 4],
151
- ]
152
- const result = toSignal(nestedArr)
147
+ ])
153
148
 
154
149
  expect(isStore(result)).toBe(true)
155
150
  // With the fixed behavior, nested arrays should be recovered as arrays
@@ -184,112 +179,6 @@ describe('toSignal', () => {
184
179
  })
185
180
  })
186
181
 
187
- describe('toMutableSignal', () => {
188
- describe('type inference and runtime behavior', () => {
189
- test('converts array to Store<Record<string, T>>', () => {
190
- const arr = [
191
- { id: 1, name: 'Alice' },
192
- { id: 2, name: 'Bob' },
193
- ]
194
- const result = toMutableSignal(arr)
195
-
196
- // Runtime behavior
197
- expect(isStore(result)).toBe(true)
198
- expect(result['0'].get()).toEqual({ id: 1, name: 'Alice' })
199
- expect(result['1'].get()).toEqual({ id: 2, name: 'Bob' })
200
-
201
- // Type inference test - now correctly returns Store<Record<string, {id: number, name: string}>>
202
- const typedResult: Store<
203
- Record<string, { id: number; name: string }>
204
- > = result
205
- expect(typedResult).toBeDefined()
206
- })
207
-
208
- test('converts record to Store<T>', () => {
209
- const record = { name: 'Alice', age: 30 }
210
- const result = toMutableSignal(record)
211
-
212
- // Runtime behavior
213
- expect(isStore(result)).toBe(true)
214
- expect(result.name.get()).toBe('Alice')
215
- expect(result.age.get()).toBe(30)
216
-
217
- // Type inference test - should be Store<{name: string, age: number}>
218
- const typedResult: Store<{ name: string; age: number }> = result
219
- expect(typedResult).toBeDefined()
220
- })
221
-
222
- test('passes through existing Store unchanged', () => {
223
- const originalStore = store({ count: 5 })
224
- const result = toMutableSignal(originalStore)
225
-
226
- // Runtime behavior
227
- expect(result).toBe(originalStore) // Should be the same instance
228
- expect(isStore(result)).toBe(true)
229
- expect(result.count.get()).toBe(5)
230
- })
231
-
232
- test('passes through existing State unchanged', () => {
233
- const originalState = state(42)
234
- const result = toMutableSignal(originalState)
235
-
236
- // Runtime behavior
237
- expect(result).toBe(originalState) // Should be the same instance
238
- expect(isState(result)).toBe(true)
239
- expect(result.get()).toBe(42)
240
-
241
- // Type inference test - should be State<number>
242
- const typedResult: State<number> = result
243
- expect(typedResult).toBeDefined()
244
- })
245
-
246
- test('converts primitive to State<T>', () => {
247
- const num = 42
248
- const result = toMutableSignal(num)
249
-
250
- // Runtime behavior - primitives are correctly converted to State
251
- expect(isState(result)).toBe(true)
252
- expect(result.get()).toBe(42)
253
- })
254
-
255
- test('converts object to State<T> (not Store)', () => {
256
- const obj = new Date('2024-01-01')
257
- const result = toMutableSignal(obj)
258
-
259
- // Runtime behavior - objects are correctly converted to State
260
- expect(isState(result)).toBe(true)
261
- expect(result.get()).toBe(obj)
262
-
263
- // Type inference test - should be State<Date>
264
- const typedResult: State<Date> = result
265
- expect(typedResult).toBeDefined()
266
- })
267
- })
268
-
269
- describe('differences from toSignal', () => {
270
- test('does not accept functions (only mutable signals)', () => {
271
- // toMutableSignal should not have a function overload
272
- // This test documents the expected behavior difference
273
- const fn = () => 'test'
274
- const result = toMutableSignal(fn)
275
-
276
- // Should treat function as a regular value and create State
277
- expect(isState(result)).toBe(true)
278
- expect(result.get()).toBe(fn)
279
- })
280
-
281
- test('does not accept Computed signals', () => {
282
- // toMutableSignal should not accept Computed signals
283
- const comp = computed(() => 'computed value')
284
- const result = toMutableSignal(comp)
285
-
286
- // Should treat Computed as a regular object and create State
287
- expect(isState(result)).toBe(true)
288
- expect(result.get()).toBe(comp)
289
- })
290
- })
291
- })
292
-
293
182
  describe('Signal compatibility', () => {
294
183
  test('all results implement Signal<T> interface', () => {
295
184
  const arraySignal = toSignal([1, 2, 3])
@@ -356,14 +245,7 @@ describe('Type precision tests', () => {
356
245
 
357
246
  describe('Type inference issues', () => {
358
247
  test('demonstrates current type inference problem', () => {
359
- // Current issue: when passing an array, T is inferred as the array type
360
- // instead of the element type, causing type compatibility problems
361
- const items = [{ id: 1 }, { id: 2 }]
362
- const result = toSignal(items)
363
-
364
- // This should work but may have type issues in external libraries
365
- // The return type should be Store<Record<string, {id: number}>>
366
- // But currently it might be inferred as Store<Record<string, {id: number}[]>>
248
+ const result = toSignal([{ id: 1 }, { id: 2 }])
367
249
 
368
250
  // Let's verify the actual behavior
369
251
  expect(isStore(result)).toBe(true)
@@ -371,7 +253,7 @@ describe('Type precision tests', () => {
371
253
  expect(result['1'].get()).toEqual({ id: 2 })
372
254
 
373
255
  // Type assertion test - this should now work with correct typing
374
- const typedResult: Store<Record<string, { id: number }>> = result
256
+ const typedResult: Store<{ id: number }[]> = result
375
257
  expect(typedResult).toBeDefined()
376
258
 
377
259
  // Simulate external library usage where P[K] represents element type
@@ -397,19 +279,11 @@ describe('Type precision tests', () => {
397
279
  })
398
280
 
399
281
  test('verifies fixed type inference for external library compatibility', () => {
400
- // This test ensures the fix for the type inference issue works
401
- // Fixed: toSignal<T extends unknown & {}>(value: T[]): Store<Record<string, T>>
402
- // Now T = {id: number} (element type), T[] = {id: number}[] (array of elements)
403
- // Return type: Store<Record<string, {id: number}>> (correct)
404
-
405
282
  const items = [
406
283
  { id: 1, name: 'Alice' },
407
284
  { id: 2, name: 'Bob' },
408
285
  ]
409
286
  const signal = toSignal(items)
410
-
411
- // Type should be Store<Record<string, {id: number, name: string}>>
412
- // Each property signal should be Signal<{id: number, name: string}>
413
287
  const firstItemSignal = signal['0']
414
288
  const secondItemSignal = signal['1']
415
289
 
@@ -419,9 +293,7 @@ describe('Type precision tests', () => {
419
293
  expect(secondItemSignal.get()).toEqual({ id: 2, name: 'Bob' })
420
294
 
421
295
  // Type inference should now work correctly:
422
- const properlyTyped: Store<
423
- Record<string, { id: number; name: string }>
424
- > = signal
296
+ const properlyTyped: Store<{ id: number; name: string }[]> = signal
425
297
  expect(properlyTyped).toBeDefined()
426
298
 
427
299
  // These should work without type errors in external libraries