@@ -21,7 +21,7 @@ npm install --save-dev simplytyped
21
21
22
22
** [ Utils] ( #utils ) **
23
23
24
- [ NoInfer] ( #noinfer ) - [ Nominal] ( #nominal ) - [ Nullable] ( #nullable ) - [ PromiseOr] ( #promiseor ) - [ UnionToIntersection] ( #uniontointersection )
24
+ [ NoDistribute ] ( #nodistribute ) - [ NoInfer] ( #noinfer ) - [ Nominal] ( #nominal ) - [ Nullable] ( #nullable ) - [ PromiseOr] ( #promiseor ) - [ UnionToIntersection] ( #uniontointersection )
25
25
26
26
** [ Functions] ( #functions ) **
27
27
@@ -522,6 +522,44 @@ test('Can get all keys between objects in a union', t => {
522
522
523
523
## Utils
524
524
525
+ ### NoDistribute
526
+ Prevent ` T ` from being distributed in a conditional type.
527
+ A conditional is only distributed when the checked type is naked type param and T & {} is not a
528
+ naked type param, but has the same contract as T.
529
+ ``` ts
530
+ test (" can create a conditional type that won't distribute over unions" , t => {
531
+ type IsString <T > = T extends string ? " Yes" : " No" ;
532
+ type IsStringNoDistribute <T > = NoDistribute <T > extends string ? " Yes" : " No" ;
533
+
534
+ /**
535
+ * Evaluates as:
536
+ * ("foo" extends string ? "Yes" : "No")
537
+ * | (42 extends string ? "Yes" : "No")
538
+ */
539
+ type T1 = IsString <" foo" | 42 >;
540
+ assert <T1 , " Yes" | " No" >(t );
541
+ assert <" Yes" | " No" , T1 >(t );
542
+
543
+ /**
544
+ * Evaluates as:
545
+ * ("foo" | 42) extends string ? "Yes" : "No"
546
+ */
547
+ type T2 = IsStringNoDistribute <" foo" | 5 >;
548
+ assert <T2 , " No" >(t );
549
+ assert <" No" , T2 >(t );
550
+ });
551
+
552
+ test (" cannot be used to prevent a distributive conditional from distributing" , t => {
553
+ type IsString <T > = T extends string ? " Yes" : " No" ;
554
+ // It's the defintion of the conditional type that matters,
555
+ // not the type that's passed in, so this still distributes
556
+ type Test = IsString <NoDistribute <" foo" | 42 >>;
557
+ assert <Test , " Yes" | " No" >(t );
558
+ assert <" Yes" | " No" , Test >(t );
559
+ });
560
+
561
+ ```
562
+
525
563
### NoInfer
526
564
Prevent ` T ` from being inferred in generic function
527
565
``` ts
0 commit comments