@xsai/stream-object 0.5.0-beta.3 → 0.5.0-beta.5

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.
package/dist/index.d.ts CHANGED
@@ -1,895 +1,9 @@
1
1
  import { StreamTextOptions, StreamTextResult } from '@xsai/stream-text';
2
2
  import { Schema } from 'xsschema';
3
3
 
4
- /**
5
- Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
6
-
7
- @category Type
8
- */
9
- type Primitive =
10
- | null
11
- | undefined
12
- | string
13
- | number
14
- | boolean
15
- | symbol
16
- | bigint;
17
-
18
- /**
19
- Returns a boolean for whether the given type is `any`.
20
-
21
- @link https://stackoverflow.com/a/49928360/1490091
22
-
23
- Useful in type utilities, such as disallowing `any`s to be passed to a function.
24
-
25
- @example
26
- ```
27
- import type {IsAny} from 'type-fest';
28
-
29
- const typedObject = {a: 1, b: 2} as const;
30
- const anyObject: any = {a: 1, b: 2};
31
-
32
- function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(object: O, key: K) {
33
- return object[key];
34
- }
35
-
36
- const typedA = get(typedObject, 'a');
37
- //=> 1
38
-
39
- const anyA = get(anyObject, 'a');
40
- //=> any
41
- ```
42
-
43
- @category Type Guard
44
- @category Utilities
45
- */
46
- type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
47
-
48
- /**
49
- Returns a boolean for whether the given key is an optional key of type.
50
-
51
- This is useful when writing utility types or schema validators that need to differentiate `optional` keys.
52
-
53
- @example
54
- ```
55
- import type {IsOptionalKeyOf} from 'type-fest';
56
-
57
- type User = {
58
- name: string;
59
- surname: string;
60
-
61
- luckyNumber?: number;
62
- };
63
-
64
- type Admin = {
65
- name: string;
66
- surname?: string;
67
- };
68
-
69
- type T1 = IsOptionalKeyOf<User, 'luckyNumber'>;
70
- //=> true
71
-
72
- type T2 = IsOptionalKeyOf<User, 'name'>;
73
- //=> false
74
-
75
- type T3 = IsOptionalKeyOf<User, 'name' | 'luckyNumber'>;
76
- //=> boolean
77
-
78
- type T4 = IsOptionalKeyOf<User | Admin, 'name'>;
79
- //=> false
80
-
81
- type T5 = IsOptionalKeyOf<User | Admin, 'surname'>;
82
- //=> boolean
83
- ```
84
-
85
- @category Type Guard
86
- @category Utilities
87
- */
88
- type IsOptionalKeyOf<Type extends object, Key extends keyof Type> =
89
- IsAny<Type | Key> extends true ? never
90
- : Key extends keyof Type
91
- ? Type extends Record<Key, Type[Key]>
92
- ? false
93
- : true
94
- : false;
95
-
96
- /**
97
- Extract all optional keys from the given type.
98
-
99
- This is useful when you want to create a new type that contains different type values for the optional keys only.
100
-
101
- @example
102
- ```
103
- import type {OptionalKeysOf, Except} from 'type-fest';
104
-
105
- type User = {
106
- name: string;
107
- surname: string;
108
-
109
- luckyNumber?: number;
110
- };
111
-
112
- const REMOVE_FIELD = Symbol('remove field symbol');
113
- type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
114
- [Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
115
- };
116
-
117
- const update1: UpdateOperation<User> = {
118
- name: 'Alice',
119
- };
120
-
121
- const update2: UpdateOperation<User> = {
122
- name: 'Bob',
123
- luckyNumber: REMOVE_FIELD,
124
- };
125
- ```
126
-
127
- @category Utilities
128
- */
129
- type OptionalKeysOf<Type extends object> =
130
- Type extends unknown // For distributing `Type`
131
- ? (keyof {[Key in keyof Type as
132
- IsOptionalKeyOf<Type, Key> extends false
133
- ? never
134
- : Key
135
- ]: never
136
- }) & keyof Type // Intersect with `keyof Type` to ensure result of `OptionalKeysOf<Type>` is always assignable to `keyof Type`
137
- : never; // Should never happen
138
-
139
- /**
140
- Extract all required keys from the given type.
141
-
142
- This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
143
-
144
- @example
145
- ```
146
- import type {RequiredKeysOf} from 'type-fest';
147
-
148
- declare function createValidation<
149
- Entity extends object,
150
- Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>,
151
- >(field: Key, validator: (value: Entity[Key]) => boolean): (entity: Entity) => boolean;
152
-
153
- type User = {
154
- name: string;
155
- surname: string;
156
- luckyNumber?: number;
157
- };
158
-
159
- const validator1 = createValidation<User>('name', value => value.length < 25);
160
- const validator2 = createValidation<User>('surname', value => value.length < 25);
161
-
162
- // @ts-expect-error
163
- const validator3 = createValidation<User>('luckyNumber', value => value > 0);
164
- // Error: Argument of type '"luckyNumber"' is not assignable to parameter of type '"name" | "surname"'.
165
- ```
166
-
167
- @category Utilities
168
- */
169
- type RequiredKeysOf<Type extends object> =
170
- Type extends unknown // For distributing `Type`
171
- ? Exclude<keyof Type, OptionalKeysOf<Type>>
172
- : never; // Should never happen
173
-
174
- /**
175
- Returns a boolean for whether the given type is `never`.
176
-
177
- @link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
178
- @link https://stackoverflow.com/a/53984913/10292952
179
- @link https://www.zhenghao.io/posts/ts-never
180
-
181
- Useful in type utilities, such as checking if something does not occur.
182
-
183
- @example
184
- ```
185
- import type {IsNever, And} from 'type-fest';
186
-
187
- type A = IsNever<never>;
188
- //=> true
189
-
190
- type B = IsNever<any>;
191
- //=> false
192
-
193
- type C = IsNever<unknown>;
194
- //=> false
195
-
196
- type D = IsNever<never[]>;
197
- //=> false
198
-
199
- type E = IsNever<object>;
200
- //=> false
201
-
202
- type F = IsNever<string>;
203
- //=> false
204
- ```
205
-
206
- @example
207
- ```
208
- import type {IsNever} from 'type-fest';
209
-
210
- type IsTrue<T> = T extends true ? true : false;
211
-
212
- // When a distributive conditional is instantiated with `never`, the entire conditional results in `never`.
213
- type A = IsTrue<never>;
214
- //=> never
215
-
216
- // If you don't want that behaviour, you can explicitly add an `IsNever` check before the distributive conditional.
217
- type IsTrueFixed<T> =
218
- IsNever<T> extends true ? false : T extends true ? true : false;
219
-
220
- type B = IsTrueFixed<never>;
221
- //=> false
222
- ```
223
-
224
- @category Type Guard
225
- @category Utilities
226
- */
227
- type IsNever<T> = [T] extends [never] ? true : false;
228
-
229
- /**
230
- An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`.
231
-
232
- Use-cases:
233
- - You can use this in combination with `Is*` types to create an if-else-like experience. For example, `If<IsAny<any>, 'is any', 'not any'>`.
234
-
235
- Note:
236
- - Returns a union of if branch and else branch if the given type is `boolean` or `any`. For example, `If<boolean, 'Y', 'N'>` will return `'Y' | 'N'`.
237
- - Returns the else branch if the given type is `never`. For example, `If<never, 'Y', 'N'>` will return `'N'`.
238
-
239
- @example
240
- ```
241
- import type {If} from 'type-fest';
242
-
243
- type A = If<true, 'yes', 'no'>;
244
- //=> 'yes'
245
-
246
- type B = If<false, 'yes', 'no'>;
247
- //=> 'no'
248
-
249
- type C = If<boolean, 'yes', 'no'>;
250
- //=> 'yes' | 'no'
251
-
252
- type D = If<any, 'yes', 'no'>;
253
- //=> 'yes' | 'no'
254
-
255
- type E = If<never, 'yes', 'no'>;
256
- //=> 'no'
257
- ```
258
-
259
- @example
260
- ```
261
- import type {If, IsAny, IsNever} from 'type-fest';
262
-
263
- type A = If<IsAny<unknown>, 'is any', 'not any'>;
264
- //=> 'not any'
265
-
266
- type B = If<IsNever<never>, 'is never', 'not never'>;
267
- //=> 'is never'
268
- ```
269
-
270
- @example
271
- ```
272
- import type {If, IsEqual} from 'type-fest';
273
-
274
- type IfEqual<T, U, IfBranch, ElseBranch> = If<IsEqual<T, U>, IfBranch, ElseBranch>;
275
-
276
- type A = IfEqual<string, string, 'equal', 'not equal'>;
277
- //=> 'equal'
278
-
279
- type B = IfEqual<string, number, 'equal', 'not equal'>;
280
- //=> 'not equal'
281
- ```
282
-
283
- Note: Sometimes using the `If` type can make an implementation non–tail-recursive, which can impact performance. In such cases, it’s better to use a conditional directly. Refer to the following example:
284
-
285
- @example
286
- ```
287
- import type {If, IsEqual, StringRepeat} from 'type-fest';
288
-
289
- type HundredZeroes = StringRepeat<'0', 100>;
290
-
291
- // The following implementation is not tail recursive
292
- type Includes<S extends string, Char extends string> =
293
- S extends `${infer First}${infer Rest}`
294
- ? If<IsEqual<First, Char>,
295
- 'found',
296
- Includes<Rest, Char>>
297
- : 'not found';
298
-
299
- // Hence, instantiations with long strings will fail
300
- // @ts-expect-error
301
- type Fails = Includes<HundredZeroes, '1'>;
302
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
303
- // Error: Type instantiation is excessively deep and possibly infinite.
304
-
305
- // However, if we use a simple conditional instead of `If`, the implementation becomes tail-recursive
306
- type IncludesWithoutIf<S extends string, Char extends string> =
307
- S extends `${infer First}${infer Rest}`
308
- ? IsEqual<First, Char> extends true
309
- ? 'found'
310
- : IncludesWithoutIf<Rest, Char>
311
- : 'not found';
312
-
313
- // Now, instantiations with long strings will work
314
- type Works = IncludesWithoutIf<HundredZeroes, '1'>;
315
- //=> 'not found'
316
- ```
317
-
318
- @category Type Guard
319
- @category Utilities
320
- */
321
- type If<Type extends boolean, IfBranch, ElseBranch> =
322
- IsNever<Type> extends true
323
- ? ElseBranch
324
- : Type extends true
325
- ? IfBranch
326
- : ElseBranch;
327
-
328
- /**
329
- Matches any primitive, `void`, `Date`, or `RegExp` value.
330
- */
331
- type BuiltIns = Primitive | void | Date | RegExp;
332
-
333
- /**
334
- Test if the given function has multiple call signatures.
335
-
336
- Needed to handle the case of a single call signature with properties.
337
-
338
- Multiple call signatures cannot currently be supported due to a TypeScript limitation.
339
- @see https://github.com/microsoft/TypeScript/issues/29732
340
- */
341
- type HasMultipleCallSignatures<T extends (...arguments_: any[]) => unknown> =
342
- T extends {(...arguments_: infer A): unknown; (...arguments_: infer B): unknown}
343
- ? B extends A
344
- ? A extends B
345
- ? false
346
- : true
347
- : true
348
- : false;
349
-
350
- /**
351
- Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
352
-
353
- @example
354
- ```
355
- import type {Simplify} from 'type-fest';
356
-
357
- type PositionProps = {
358
- top: number;
359
- left: number;
360
- };
361
-
362
- type SizeProps = {
363
- width: number;
364
- height: number;
365
- };
366
-
367
- // In your editor, hovering over `Props` will show a flattened object with all the properties.
368
- type Props = Simplify<PositionProps & SizeProps>;
369
- ```
370
-
371
- Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
372
-
373
- If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
374
-
375
- @example
376
- ```
377
- import type {Simplify} from 'type-fest';
378
-
379
- interface SomeInterface {
380
- foo: number;
381
- bar?: string;
382
- baz: number | undefined;
383
- }
384
-
385
- type SomeType = {
386
- foo: number;
387
- bar?: string;
388
- baz: number | undefined;
389
- };
390
-
391
- const literal = {foo: 123, bar: 'hello', baz: 456};
392
- const someType: SomeType = literal;
393
- const someInterface: SomeInterface = literal;
394
-
395
- declare function fn(object: Record<string, unknown>): void;
396
-
397
- fn(literal); // Good: literal object type is sealed
398
- fn(someType); // Good: type is sealed
399
- // @ts-expect-error
400
- fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
401
- fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
402
- ```
403
-
404
- @link https://github.com/microsoft/TypeScript/issues/15300
405
- @see {@link SimplifyDeep}
406
- @category Object
407
- */
408
- type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
409
-
410
- /**
411
- Returns a boolean for whether the two given types are equal.
412
-
413
- @link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
414
- @link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
415
-
416
- Use-cases:
417
- - If you want to make a conditional branch based on the result of a comparison of two types.
418
-
419
- @example
420
- ```
421
- import type {IsEqual} from 'type-fest';
422
-
423
- // This type returns a boolean for whether the given array includes the given item.
424
- // `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
425
- type Includes<Value extends readonly any[], Item> =
426
- Value extends readonly [Value[0], ...infer rest]
427
- ? IsEqual<Value[0], Item> extends true
428
- ? true
429
- : Includes<rest, Item>
430
- : false;
431
- ```
432
-
433
- @category Type Guard
434
- @category Utilities
435
- */
436
- type IsEqual<A, B> =
437
- [A] extends [B]
438
- ? [B] extends [A]
439
- ? _IsEqual<A, B>
440
- : false
441
- : false;
442
-
443
- // This version fails the `equalWrappedTupleIntersectionToBeNeverAndNeverExpanded` test in `test-d/is-equal.ts`.
444
- type _IsEqual<A, B> =
445
- (<G>() => G extends A & G | G ? 1 : 2) extends
446
- (<G>() => G extends B & G | G ? 1 : 2)
447
- ? true
448
- : false;
449
-
450
- /**
451
- Omit any index signatures from the given object type, leaving only explicitly defined properties.
452
-
453
- This is the counterpart of `PickIndexSignature`.
454
-
455
- Use-cases:
456
- - Remove overly permissive signatures from third-party types.
457
-
458
- This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
459
-
460
- It relies on the fact that an empty object (`{}`) is assignable to an object with just an index signature, like `Record<string, unknown>`, but not to an object with explicitly defined keys, like `Record<'foo' | 'bar', unknown>`.
461
-
462
- (The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
463
-
464
- ```
465
- const indexed: Record<string, unknown> = {}; // Allowed
466
-
467
- // @ts-expect-error
468
- const keyed: Record<'foo', unknown> = {}; // Error
469
- // TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
470
- ```
471
-
472
- Instead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
473
-
474
- ```
475
- type Indexed = {} extends Record<string, unknown>
476
- ? '✅ `{}` is assignable to `Record<string, unknown>`'
477
- : '❌ `{}` is NOT assignable to `Record<string, unknown>`';
478
-
479
- type IndexedResult = Indexed;
480
- //=> '✅ `{}` is assignable to `Record<string, unknown>`'
481
-
482
- type Keyed = {} extends Record<'foo' | 'bar', unknown>
483
- ? '✅ `{}` is assignable to `Record<\'foo\' | \'bar\', unknown>`'
484
- : '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`';
485
-
486
- type KeyedResult = Keyed;
487
- //=> '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`'
488
- ```
489
-
490
- Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
491
-
492
- ```
493
- type OmitIndexSignature<ObjectType> = {
494
- [KeyType in keyof ObjectType // Map each key of `ObjectType`...
495
- ]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
496
- };
497
- ```
498
-
499
- ...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
500
-
501
- ```
502
- type OmitIndexSignature<ObjectType> = {
503
- [KeyType in keyof ObjectType
504
- // Is `{}` assignable to `Record<KeyType, unknown>`?
505
- as {} extends Record<KeyType, unknown>
506
- ? never // ✅ `{}` is assignable to `Record<KeyType, unknown>`
507
- : KeyType // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
508
- ]: ObjectType[KeyType];
509
- };
510
- ```
511
-
512
- If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
513
-
514
- @example
515
- ```
516
- import type {OmitIndexSignature} from 'type-fest';
517
-
518
- type Example = {
519
- // These index signatures will be removed.
520
- [x: string]: any;
521
- [x: number]: any;
522
- [x: symbol]: any;
523
- [x: `head-${string}`]: string;
524
- [x: `${string}-tail`]: string;
525
- [x: `head-${string}-tail`]: string;
526
- [x: `${bigint}`]: string;
527
- [x: `embedded-${number}`]: string;
528
-
529
- // These explicitly defined keys will remain.
530
- foo: 'bar';
531
- qux?: 'baz';
532
- };
533
-
534
- type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
535
- //=> {foo: 'bar'; qux?: 'baz'}
536
- ```
537
-
538
- @see {@link PickIndexSignature}
539
- @category Object
540
- */
541
- type OmitIndexSignature<ObjectType> = {
542
- [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
543
- ? never
544
- : KeyType]: ObjectType[KeyType];
545
- };
546
-
547
- /**
548
- Pick only index signatures from the given object type, leaving out all explicitly defined properties.
549
-
550
- This is the counterpart of `OmitIndexSignature`.
551
-
552
- @example
553
- ```
554
- import type {PickIndexSignature} from 'type-fest';
555
-
556
- declare const symbolKey: unique symbol;
557
-
558
- type Example = {
559
- // These index signatures will remain.
560
- [x: string]: unknown;
561
- [x: number]: unknown;
562
- [x: symbol]: unknown;
563
- [x: `head-${string}`]: string;
564
- [x: `${string}-tail`]: string;
565
- [x: `head-${string}-tail`]: string;
566
- [x: `${bigint}`]: string;
567
- [x: `embedded-${number}`]: string;
568
-
569
- // These explicitly defined keys will be removed.
570
- ['kebab-case-key']: string;
571
- [symbolKey]: string;
572
- foo: 'bar';
573
- qux?: 'baz';
574
- };
575
-
576
- type ExampleIndexSignature = PickIndexSignature<Example>;
577
- // {
578
- // [x: string]: unknown;
579
- // [x: number]: unknown;
580
- // [x: symbol]: unknown;
581
- // [x: `head-${string}`]: string;
582
- // [x: `${string}-tail`]: string;
583
- // [x: `head-${string}-tail`]: string;
584
- // [x: `${bigint}`]: string;
585
- // [x: `embedded-${number}`]: string;
586
- // }
587
- ```
588
-
589
- @see {@link OmitIndexSignature}
590
- @category Object
591
- */
592
- type PickIndexSignature<ObjectType> = {
593
- [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
594
- ? KeyType
595
- : never]: ObjectType[KeyType];
596
- };
597
-
598
- // Merges two objects without worrying about index signatures.
599
- type SimpleMerge<Destination, Source> = Simplify<{
600
- [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
601
- } & Source>;
602
-
603
- /**
604
- Merge two types into a new type. Keys of the second type overrides keys of the first type.
605
-
606
- This is different from the TypeScript `&` (intersection) operator. With `&`, conflicting property types are intersected, which often results in `never`. For example, `{a: string} & {a: number}` makes `a` become `string & number`, which resolves to `never`. With `Merge`, the second type's keys cleanly override the first, so `Merge<{a: string}, {a: number}>` gives `{a: number}` as expected. `Merge` also produces a flattened type (via `Simplify`), making it more readable in IDE tooltips compared to `A & B`.
607
-
608
- @example
609
- ```
610
- import type {Merge} from 'type-fest';
611
-
612
- type Foo = {
613
- a: string;
614
- b: number;
615
- };
616
-
617
- type Bar = {
618
- a: number; // Conflicts with Foo['a']
619
- c: boolean;
620
- };
621
-
622
- // With `&`, `a` becomes `string & number` which is `never`. Not what you want.
623
- type WithIntersection = (Foo & Bar)['a'];
624
- //=> never
625
-
626
- // With `Merge`, `a` is cleanly overridden to `number`.
627
- type WithMerge = Merge<Foo, Bar>['a'];
628
- //=> number
629
- ```
630
-
631
- @example
632
- ```
633
- import type {Merge} from 'type-fest';
634
-
635
- type Foo = {
636
- [x: string]: unknown;
637
- [x: number]: unknown;
638
- foo: string;
639
- bar: symbol;
640
- };
641
-
642
- type Bar = {
643
- [x: number]: number;
644
- [x: symbol]: unknown;
645
- bar: Date;
646
- baz: boolean;
647
- };
648
-
649
- export type FooBar = Merge<Foo, Bar>;
650
- //=> {
651
- // [x: string]: unknown;
652
- // [x: number]: number;
653
- // [x: symbol]: unknown;
654
- // foo: string;
655
- // bar: Date;
656
- // baz: boolean;
657
- // }
658
- ```
659
-
660
- Note: If you want a merge type that more accurately reflects the runtime behavior of object spread or `Object.assign`, refer to the {@link ObjectMerge} type.
661
-
662
- @see {@link ObjectMerge}
663
- @category Object
664
- */
665
- type Merge<Destination, Source> =
666
- Destination extends unknown // For distributing `Destination`
667
- ? Source extends unknown // For distributing `Source`
668
- ? If<IsEqual<Destination, Source>, Destination, _Merge<Destination, Source>>
669
- : never // Should never happen
670
- : never; // Should never happen
671
-
672
- type _Merge<Destination, Source> =
673
- Simplify<
674
- SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
675
- & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
676
- >;
677
-
678
- /**
679
- Merges user specified options with default options.
680
-
681
- @example
682
- ```
683
- type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
684
- type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
685
- type SpecifiedOptions = {leavesOnly: true};
686
-
687
- type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
688
- //=> {maxRecursionDepth: 10; leavesOnly: true}
689
- ```
690
-
691
- @example
692
- ```
693
- // Complains if default values are not provided for optional options
694
-
695
- type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
696
- type DefaultPathsOptions = {maxRecursionDepth: 10};
697
- type SpecifiedOptions = {};
698
-
699
- type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
700
- // ~~~~~~~~~~~~~~~~~~~
701
- // Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
702
- ```
703
-
704
- @example
705
- ```
706
- // Complains if an option's default type does not conform to the expected type
707
-
708
- type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
709
- type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
710
- type SpecifiedOptions = {};
711
-
712
- type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
713
- // ~~~~~~~~~~~~~~~~~~~
714
- // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
715
- ```
716
-
717
- @example
718
- ```
719
- // Complains if an option's specified type does not conform to the expected type
720
-
721
- type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
722
- type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
723
- type SpecifiedOptions = {leavesOnly: 'yes'};
724
-
725
- type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
726
- // ~~~~~~~~~~~~~~~~
727
- // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
728
- ```
729
- */
730
- type ApplyDefaultOptions<
731
- Options extends object,
732
- Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
733
- SpecifiedOptions extends Options,
734
- > =
735
- If<IsAny<SpecifiedOptions>, Defaults,
736
- If<IsNever<SpecifiedOptions>, Defaults,
737
- Simplify<Merge<Defaults, {
738
- [Key in keyof SpecifiedOptions
739
- as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
740
- ]: SpecifiedOptions[Key]
741
- }> & Required<Options>>>>;
742
-
743
- /**
744
- @see {@link PartialDeep}
745
- */
746
- type PartialDeepOptions = {
747
- /**
748
- Whether to affect the individual elements of arrays and tuples.
749
-
750
- @default false
751
- */
752
- readonly recurseIntoArrays?: boolean;
753
-
754
- /**
755
- Allows `undefined` values in non-tuple arrays.
756
-
757
- - When set to `true`, elements of non-tuple arrays can be `undefined`.
758
- - When set to `false`, only explicitly defined elements are allowed in non-tuple arrays, ensuring stricter type checking.
759
-
760
- @default false
761
-
762
- @example
763
- You can allow `undefined` values in non-tuple arrays by passing `{recurseIntoArrays: true; allowUndefinedInNonTupleArrays: true}` as the second type argument:
764
-
765
- ```
766
- import type {PartialDeep} from 'type-fest';
767
-
768
- type Settings = {
769
- languages: string[];
770
- };
771
-
772
- declare const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true; allowUndefinedInNonTupleArrays: true}>;
773
-
774
- partialSettings.languages = [undefined]; // OK
775
- ```
776
- */
777
- readonly allowUndefinedInNonTupleArrays?: boolean;
778
- };
779
-
780
- type DefaultPartialDeepOptions = {
781
- recurseIntoArrays: false;
782
- allowUndefinedInNonTupleArrays: false;
783
- };
784
-
785
- /**
786
- Create a type from another type with all keys and nested keys set to optional.
787
-
788
- Use-cases:
789
- - Merging a default settings/config object with another object, the second object would be a deep partial of the default object.
790
- - Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.
791
-
792
- @example
793
- ```
794
- import type {PartialDeep} from 'type-fest';
795
-
796
- let settings = {
797
- textEditor: {
798
- fontSize: 14,
799
- fontColor: '#000000',
800
- fontWeight: 400,
801
- },
802
- autocomplete: false,
803
- autosave: true,
804
- };
805
-
806
- const applySavedSettings = (savedSettings: PartialDeep<typeof settings>) => (
807
- {...settings, ...savedSettings, textEditor: {...settings.textEditor, ...savedSettings.textEditor}}
808
- );
809
-
810
- settings = applySavedSettings({textEditor: {fontWeight: 500}});
811
- ```
812
-
813
- By default, this does not affect elements in array and tuple types. You can change this by passing `{recurseIntoArrays: true}` as the second type argument:
814
-
815
- ```
816
- import type {PartialDeep} from 'type-fest';
817
-
818
- type Shape = {
819
- dimensions: [number, number];
820
- };
821
-
822
- const partialShape: PartialDeep<Shape, {recurseIntoArrays: true}> = {
823
- dimensions: [], // OK
824
- };
825
-
826
- partialShape.dimensions = [15]; // OK
827
- ```
828
-
829
- @see {@link PartialDeepOptions}
830
-
831
- @category Object
832
- @category Array
833
- @category Set
834
- @category Map
835
- */
836
- type PartialDeep<T, Options extends PartialDeepOptions = {}> =
837
- _PartialDeep<T, ApplyDefaultOptions<PartialDeepOptions, DefaultPartialDeepOptions, Options>>;
838
-
839
- type _PartialDeep<T, Options extends Required<PartialDeepOptions>> = T extends BuiltIns | ((new (...arguments_: any[]) => unknown))
840
- ? T
841
- : T extends Map<infer KeyType, infer ValueType>
842
- ? PartialMapDeep<KeyType, ValueType, Options>
843
- : T extends Set<infer ItemType>
844
- ? PartialSetDeep<ItemType, Options>
845
- : T extends ReadonlyMap<infer KeyType, infer ValueType>
846
- ? PartialReadonlyMapDeep<KeyType, ValueType, Options>
847
- : T extends ReadonlySet<infer ItemType>
848
- ? PartialReadonlySetDeep<ItemType, Options>
849
- : T extends (...arguments_: any[]) => unknown
850
- ? IsNever<keyof T> extends true
851
- ? T // For functions with no properties
852
- : HasMultipleCallSignatures<T> extends true
853
- ? T
854
- : ((...arguments_: Parameters<T>) => ReturnType<T>) & PartialObjectDeep<T, Options>
855
- : T extends object
856
- ? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
857
- ? Options['recurseIntoArrays'] extends true
858
- ? ItemType[] extends T // Test for arrays (non-tuples) specifically
859
- ? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
860
- ? ReadonlyArray<_PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
861
- : Array<_PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
862
- : PartialObjectDeep<T, Options> // Tuples behave properly
863
- : T // If they don't opt into array testing, just use the original type
864
- : PartialObjectDeep<T, Options>
865
- : unknown;
866
-
867
- /**
868
- Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
869
- */
870
- type PartialMapDeep<KeyType, ValueType, Options extends Required<PartialDeepOptions>> = {} & Map<_PartialDeep<KeyType, Options>, _PartialDeep<ValueType, Options>>;
871
-
872
- /**
873
- Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
874
- */
875
- type PartialSetDeep<T, Options extends Required<PartialDeepOptions>> = {} & Set<_PartialDeep<T, Options>>;
876
-
877
- /**
878
- Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
879
- */
880
- type PartialReadonlyMapDeep<KeyType, ValueType, Options extends Required<PartialDeepOptions>> = {} & ReadonlyMap<_PartialDeep<KeyType, Options>, _PartialDeep<ValueType, Options>>;
881
-
882
- /**
883
- Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
884
- */
885
- type PartialReadonlySetDeep<T, Options extends Required<PartialDeepOptions>> = {} & ReadonlySet<_PartialDeep<T, Options>>;
886
-
887
- /**
888
- Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
889
- */
890
- type PartialObjectDeep<ObjectType extends object, Options extends Required<PartialDeepOptions>> = {
891
- [KeyType in keyof ObjectType]?: _PartialDeep<ObjectType[KeyType], Options>
892
- };
4
+ type PartialDeep<T> = T extends readonly (infer U)[] ? readonly PartialDeep<U>[] : T extends object ? {
5
+ [K in keyof T]?: PartialDeep<T[K]>;
6
+ } : T;
893
7
 
894
8
  interface StreamObjectOnFinishResult<T extends Schema> {
895
9
  object?: Schema.InferOutput<T>;
package/dist/index.js CHANGED
@@ -426,6 +426,7 @@ async function streamObject(options) {
426
426
  schema = wrap(schema);
427
427
  let { textStream, ...rest } = streamText({
428
428
  ...options,
429
+ output: void 0,
429
430
  response_format: {
430
431
  json_schema: {
431
432
  description: options.schemaDescription,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xsai/stream-object",
3
3
  "type": "module",
4
- "version": "0.5.0-beta.3",
4
+ "version": "0.5.0-beta.5",
5
5
  "description": "extra-small AI SDK.",
6
6
  "author": "Moeru AI",
7
7
  "license": "MIT",
@@ -29,13 +29,12 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@xsai/stream-text": "0.5.0-beta.3",
33
- "xsschema": "0.5.0-beta.3"
32
+ "@xsai/stream-text": "0.5.0-beta.5",
33
+ "xsschema": "0.5.0-beta.5"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@valibot/to-json-schema": "^1.0.0",
37
37
  "best-effort-json-parser": "^1.4.0",
38
- "type-fest": "^5.5.0",
39
38
  "valibot": "^1.0.0"
40
39
  },
41
40
  "scripts": {