simplytyped
Advanced tools
Comparing version 3.0.0 to 3.1.0
{ | ||
"name": "simplytyped", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "yet another Typescript type library for advanced types", | ||
@@ -36,4 +36,4 @@ "main": "index", | ||
"@commitlint/config-conventional": "^7.0.0", | ||
"@types/node": "~11.11.0", | ||
"ava": "~1.3.0", | ||
"@types/node": "~11.13.1", | ||
"ava": "~1.4.0", | ||
"commitlint": "^7.0.0", | ||
@@ -40,0 +40,0 @@ "husky": "^1.0.0", |
@@ -24,3 +24,3 @@ # SimplyTyped | ||
[NoInfer](#noinfer) - [Nominal](#nominal) - [Nullable](#nullable) - [PromiseOr](#promiseor) | ||
[NoInfer](#noinfer) - [Nominal](#nominal) - [Nullable](#nullable) - [PromiseOr](#promiseor) - [UnionToIntersection](#uniontointersection) | ||
@@ -585,2 +585,26 @@ **[Functions](#functions)** | ||
### UnionToIntersection | ||
Defines an intersection type of all union items. | ||
```ts | ||
test('Union of Strings', t => { | ||
type got = UnionToIntersection<'hi' | 'there'>; | ||
type expected = 'hi' & 'there'; | ||
assert<got, expected>(t); | ||
}); | ||
test('Union of Objects', t => { | ||
type got = UnionToIntersection<{ a: 0 } | { b: 1 } | { c: 2 }>; | ||
type expected = { | ||
a: 0, | ||
b: 1, | ||
c: 2, | ||
}; | ||
assert<got, expected>(t); | ||
}); | ||
``` | ||
## Functions | ||
@@ -587,0 +611,0 @@ |
@@ -0,1 +1,2 @@ | ||
import { UnionToIntersection } from './utils'; | ||
export interface Vector<T> { | ||
@@ -14,5 +15,5 @@ readonly [x: number]: T; | ||
* Gives an intersection of all values contained in a tuple. | ||
* @param T a tuple of items up to 10 | ||
* @param T a tuple of items | ||
* @returns an intersection of all items in the tuple | ||
*/ | ||
export declare type IntersectTuple<T extends Vector<any>> = Length<T> extends 1 ? T[0] : Length<T> extends 2 ? T[0] & T[1] : Length<T> extends 3 ? T[0] & T[1] & T[2] : Length<T> extends 4 ? T[0] & T[1] & T[2] & T[3] : Length<T> extends 5 ? T[0] & T[1] & T[2] & T[3] & T[4] : Length<T> extends 6 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] : Length<T> extends 7 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] : Length<T> extends 8 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] & T[7] : Length<T> extends 9 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] & T[7] & T[8] : Length<T> extends 10 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] & T[7] & T[8] & T[9] : any; | ||
export declare type IntersectTuple<T extends Vector<any>> = UnionToIntersection<T[number]>; |
@@ -36,1 +36,8 @@ import { ObjectType } from "./objects"; | ||
export declare type PromiseOr<T> = Promise<T> | T; | ||
/** | ||
* Defines an intersection type of all union items. | ||
* @param U Union of any types that will be intersected. | ||
* @returns U items intersected | ||
* @see https://stackoverflow.com/a/50375286/9259330 | ||
*/ | ||
export declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never; |
50525
522
1066