@xsai/stream-object 0.4.2 → 0.4.3
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 +67 -13
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -211,14 +211,14 @@ type IsTrue<T> = T extends true ? true : false;
|
|
|
211
211
|
|
|
212
212
|
// When a distributive conditional is instantiated with `never`, the entire conditional results in `never`.
|
|
213
213
|
type A = IsTrue<never>;
|
|
214
|
-
|
|
214
|
+
//=> never
|
|
215
215
|
|
|
216
216
|
// If you don't want that behaviour, you can explicitly add an `IsNever` check before the distributive conditional.
|
|
217
217
|
type IsTrueFixed<T> =
|
|
218
218
|
IsNever<T> extends true ? false : T extends true ? true : false;
|
|
219
219
|
|
|
220
220
|
type B = IsTrueFixed<never>;
|
|
221
|
-
|
|
221
|
+
//=> false
|
|
222
222
|
```
|
|
223
223
|
|
|
224
224
|
@category Type Guard
|
|
@@ -407,6 +407,46 @@ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface`
|
|
|
407
407
|
*/
|
|
408
408
|
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
409
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
|
+
|
|
410
450
|
/**
|
|
411
451
|
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
412
452
|
|
|
@@ -426,7 +466,7 @@ const indexed: Record<string, unknown> = {}; // Allowed
|
|
|
426
466
|
|
|
427
467
|
// @ts-expect-error
|
|
428
468
|
const keyed: Record<'foo', unknown> = {}; // Error
|
|
429
|
-
//
|
|
469
|
+
// TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
430
470
|
```
|
|
431
471
|
|
|
432
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:
|
|
@@ -435,12 +475,16 @@ Instead of causing a type error like the above, you can also use a [conditional
|
|
|
435
475
|
type Indexed = {} extends Record<string, unknown>
|
|
436
476
|
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
437
477
|
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
438
|
-
|
|
478
|
+
|
|
479
|
+
type IndexedResult = Indexed;
|
|
480
|
+
//=> '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
439
481
|
|
|
440
482
|
type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
441
483
|
? '✅ `{}` is assignable to `Record<\'foo\' | \'bar\', unknown>`'
|
|
442
484
|
: '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`';
|
|
443
|
-
|
|
485
|
+
|
|
486
|
+
type KeyedResult = Keyed;
|
|
487
|
+
//=> '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`'
|
|
444
488
|
```
|
|
445
489
|
|
|
446
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`...
|
|
@@ -488,7 +532,7 @@ type Example = {
|
|
|
488
532
|
};
|
|
489
533
|
|
|
490
534
|
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
491
|
-
|
|
535
|
+
//=> {foo: 'bar'; qux?: 'baz'}
|
|
492
536
|
```
|
|
493
537
|
|
|
494
538
|
@see {@link PickIndexSignature}
|
|
@@ -552,9 +596,9 @@ type PickIndexSignature<ObjectType> = {
|
|
|
552
596
|
};
|
|
553
597
|
|
|
554
598
|
// Merges two objects without worrying about index signatures.
|
|
555
|
-
type SimpleMerge<Destination, Source> = {
|
|
599
|
+
type SimpleMerge<Destination, Source> = Simplify<{
|
|
556
600
|
[Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
|
|
557
|
-
} & Source
|
|
601
|
+
} & Source>;
|
|
558
602
|
|
|
559
603
|
/**
|
|
560
604
|
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
@@ -578,7 +622,7 @@ type Bar = {
|
|
|
578
622
|
};
|
|
579
623
|
|
|
580
624
|
export type FooBar = Merge<Foo, Bar>;
|
|
581
|
-
|
|
625
|
+
//=> {
|
|
582
626
|
// [x: string]: unknown;
|
|
583
627
|
// [x: number]: number;
|
|
584
628
|
// [x: symbol]: unknown;
|
|
@@ -588,13 +632,23 @@ export type FooBar = Merge<Foo, Bar>;
|
|
|
588
632
|
// }
|
|
589
633
|
```
|
|
590
634
|
|
|
635
|
+
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.
|
|
636
|
+
|
|
637
|
+
@see {@link ObjectMerge}
|
|
591
638
|
@category Object
|
|
592
639
|
*/
|
|
593
640
|
type Merge<Destination, Source> =
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
641
|
+
Destination extends unknown // For distributing `Destination`
|
|
642
|
+
? Source extends unknown // For distributing `Source`
|
|
643
|
+
? If<IsEqual<Destination, Source>, Destination, _Merge<Destination, Source>>
|
|
644
|
+
: never // Should never happen
|
|
645
|
+
: never; // Should never happen
|
|
646
|
+
|
|
647
|
+
type _Merge<Destination, Source> =
|
|
648
|
+
Simplify<
|
|
649
|
+
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
|
|
650
|
+
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
|
|
651
|
+
>;
|
|
598
652
|
|
|
599
653
|
/**
|
|
600
654
|
Merges user specified options with default options.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/stream-object",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.3",
|
|
5
5
|
"description": "extra-small AI SDK.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -29,13 +29,13 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@xsai/stream-text": "~0.4.
|
|
33
|
-
"xsschema": "~0.4.
|
|
32
|
+
"@xsai/stream-text": "~0.4.3",
|
|
33
|
+
"xsschema": "~0.4.3"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@valibot/to-json-schema": "^1.0.0",
|
|
37
37
|
"best-effort-json-parser": "^1.2.1",
|
|
38
|
-
"type-fest": "^5.3
|
|
38
|
+
"type-fest": "^5.4.3",
|
|
39
39
|
"valibot": "^1.0.0"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|