simplytyped
Advanced tools
Comparing version 3.2.0 to 3.2.1
{ | ||
"name": "simplytyped", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"description": "yet another Typescript type library for advanced types", | ||
@@ -5,0 +5,0 @@ "main": "index", |
@@ -24,3 +24,3 @@ # SimplyTyped | ||
[NoInfer](#noinfer) - [Nominal](#nominal) - [Nullable](#nullable) - [PromiseOr](#promiseor) - [UnionToIntersection](#uniontointersection) | ||
[NoDistribute](#nodistribute) - [NoInfer](#noinfer) - [Nominal](#nominal) - [Nullable](#nullable) - [PromiseOr](#promiseor) - [UnionToIntersection](#uniontointersection) | ||
@@ -526,2 +526,40 @@ **[Functions](#functions)** | ||
### NoDistribute | ||
Prevent `T` from being distributed in a conditional type. | ||
A conditional is only distributed when the checked type is naked type param and T & {} is not a | ||
naked type param, but has the same contract as T. | ||
```ts | ||
test("can create a conditional type that won't distribute over unions", t => { | ||
type IsString<T> = T extends string ? "Yes" : "No"; | ||
type IsStringNoDistribute<T> = NoDistribute<T> extends string ? "Yes" : "No"; | ||
/** | ||
* Evaluates as: | ||
* ("foo" extends string ? "Yes" : "No") | ||
* | (42 extends string ? "Yes" : "No") | ||
*/ | ||
type T1 = IsString<"foo" | 42>; | ||
assert<T1, "Yes" | "No">(t); | ||
assert<"Yes" | "No", T1>(t); | ||
/** | ||
* Evaluates as: | ||
* ("foo" | 42) extends string ? "Yes" : "No" | ||
*/ | ||
type T2 = IsStringNoDistribute<"foo" | 5>; | ||
assert<T2, "No">(t); | ||
assert<"No", T2>(t); | ||
}); | ||
test("cannot be used to prevent a distributive conditional from distributing", t => { | ||
type IsString<T> = T extends string ? "Yes" : "No"; | ||
// It's the defintion of the conditional type that matters, | ||
// not the type that's passed in, so this still distributes | ||
type Test = IsString<NoDistribute<"foo" | 42>>; | ||
assert<Test, "Yes" | "No">(t); | ||
assert<"Yes" | "No", Test>(t); | ||
}); | ||
``` | ||
### NoInfer | ||
@@ -528,0 +566,0 @@ Prevent `T` from being inferred in generic function |
@@ -28,2 +28,2 @@ import { False, True, And, Or, Not } from './conditionals'; | ||
export declare type IsObject<T> = And<T extends object ? True : False, And<Not<IsFunction<T>>, Not<IsArray<T>>>>; | ||
export declare type IsAny<T> = And<Not<IsArray<T>>, And<Not<IsBoolean<T>>, And<Not<IsNumber<T>>, And<Not<IsString<T>>, And<Not<IsFunction<T>>, And<Not<IsNil<T>>, Not<IsObject<T>>>>>>>>; | ||
export declare type IsAny<T> = 0 extends (1 & T) ? True : False; |
52238
1104