simplytyped
Advanced tools
Comparing version 1.8.0 to 2.0.0
@@ -1,2 +0,2 @@ | ||
import { DeepReadonly, TaggedObject, ObjectKeys } from '../types/objects'; | ||
import { DeepReadonly, TaggedObject } from '../types/objects'; | ||
/** | ||
@@ -29,2 +29,2 @@ * Type guard for any key, `k`. | ||
*/ | ||
export declare function taggedObject<T extends Record<ObjectKeys, object>, K extends string>(obj: T, key: K): TaggedObject<T, K>; | ||
export declare function taggedObject<T extends Record<keyof any, object>, K extends string>(obj: T, key: K): TaggedObject<T, K>; |
{ | ||
"name": "simplytyped", | ||
"version": "1.8.0", | ||
"version": "2.0.0", | ||
"description": "yet another Typescript type library for advanced types", | ||
@@ -36,8 +36,8 @@ "main": "index", | ||
"@commitlint/config-conventional": "^7.0.0", | ||
"@types/node": "~11.9.0", | ||
"ava": "~1.2.1", | ||
"@types/node": "~11.11.0", | ||
"ava": "~1.3.0", | ||
"commitlint": "^7.0.0", | ||
"husky": "^1.0.0", | ||
"ts-node": "^7.0.0", | ||
"tslint": "^5.8.0" | ||
"ts-node": "^8.0.3", | ||
"tslint": "^5.13.0" | ||
}, | ||
@@ -44,0 +44,0 @@ "ava": { |
@@ -20,7 +20,7 @@ # SimplyTyped | ||
[AllKeys](#allkeys) - [AllRequired](#allrequired) - [CombineObjects](#combineobjects) - [DeepPartial](#deeppartial) - [DeepReadonly](#deepreadonly) - [DiffKeys](#diffkeys) - [GetKey](#getkey) - [HasKey](#haskey) - [Intersect](#intersect) - [Keys](#keys) - [KeysByType](#keysbytype) - [Merge](#merge) - [ObjectKeys](#objectkeys) - [ObjectType](#objecttype) - [Omit](#omit) - [Optional](#optional) - [Overwrite](#overwrite) - [PlainObject](#plainobject) - [PureKeys](#purekeys) - [Required](#required) - [SharedKeys](#sharedkeys) - [StrictUnion](#strictunion) - [StringKeys](#stringkeys) - [TaggedObject](#taggedobject) - [UnionizeProperties](#unionizeproperties) - [UnionKeys](#unionkeys) | ||
[AllKeys](#allkeys) - [AllRequired](#allrequired) - [CombineObjects](#combineobjects) - [DeepPartial](#deeppartial) - [DeepReadonly](#deepreadonly) - [DiffKeys](#diffkeys) - [GetKey](#getkey) - [HasKey](#haskey) - [Intersect](#intersect) - [KeysByType](#keysbytype) - [Merge](#merge) - [ObjectKeys](#objectkeys) - [ObjectType](#objecttype) - [Omit](#omit) - [Optional](#optional) - [Overwrite](#overwrite) - [PlainObject](#plainobject) - [PureKeys](#purekeys) - [Required](#required) - [SharedKeys](#sharedkeys) - [StrictUnion](#strictunion) - [StringKeys](#stringkeys) - [TaggedObject](#taggedobject) - [UnionizeProperties](#unionizeproperties) - [UnionKeys](#unionkeys) | ||
**[Utils](#utils)** | ||
[NoInfer](#noinfer) - [Nominal](#nominal) - [Nullable](#nullable) - [PromiseOr](#promiseor) - [Unknown](#unknown) | ||
[NoInfer](#noinfer) - [Nominal](#nominal) - [Nullable](#nullable) - [PromiseOr](#promiseor) | ||
@@ -33,3 +33,3 @@ **[Functions](#functions)** | ||
[Diff](#diff) - [DropString](#dropstring) - [StringEqual](#stringequal) - [UnionContains](#unioncontains) | ||
[DropString](#dropstring) - [StringEqual](#stringequal) - [UnionContains](#unioncontains) | ||
@@ -226,3 +226,3 @@ **[Tuples](#tuples)** | ||
Gets all of the keys that are different between two objects. | ||
This is a set difference between `Keys<T>` and `Keys<U>`. | ||
This is a set difference between `keyof T` and `keyof U`. | ||
Note that calling this with arguments reversed will have different results. | ||
@@ -289,16 +289,2 @@ ```ts | ||
### Keys | ||
No different than `keyof`, but can look a bit nicer when nesting many types deep. | ||
```ts | ||
test('Can get keys from object', t => { | ||
type obj = { x: number, y: string, z: boolean }; | ||
type expected = 'x' | 'y' | 'z'; | ||
type got = Keys<obj>; | ||
assert<got, expected>(t); | ||
assert<expected, got>(t); | ||
}); | ||
``` | ||
### KeysByType | ||
@@ -602,6 +588,2 @@ Gets all keys that point to a given type. | ||
### Unknown | ||
A constant type that mimics an unknowable type. | ||
## Functions | ||
@@ -635,3 +617,3 @@ | ||
type E0 = void[]; | ||
type E0 = []; | ||
type E1 = [number]; | ||
@@ -700,18 +682,2 @@ type E2 = [number, string]; | ||
### Diff | ||
```ts | ||
test('Can get difference between unions of strings', t => { | ||
type a = 'hi' | 'there'; | ||
type b = 'hi' | 'my' | 'friend'; | ||
type gotA = Diff<a, b>; | ||
type gotB = Diff<b, a>; | ||
assert<gotA, 'there'>(t); | ||
assert<gotB, 'my' | 'friend'>(t); | ||
}); | ||
``` | ||
### DropString | ||
@@ -718,0 +684,0 @@ |
@@ -22,3 +22,3 @@ import { If } from './conditionals'; | ||
*/ | ||
export declare type OverwriteReturn<F extends AnyFunc, R> = F extends () => any ? () => R : F extends (x1: infer X1) => any ? (x1: X1) => R : F extends (x1: infer X1, x2: infer X2) => any ? (x1: X1, x2: X2) => R : F extends (x1: infer X1, x2: infer X2, x3: infer X3) => any ? (x1: X1, x2: X2, x3: X3) => R : F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4) => any ? (x1: X1, x2: X2, x3: X3, x4: X4) => R : F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5) => any ? (x1: X1, x2: X2, x3: X3, x4: X4, x5: X5) => R : F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5, x6: infer X6) => any ? (x1: X1, x2: X2, x3: X3, x4: X4, x5: X5, x6: X6) => R : F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5, x6: infer X6, x7: infer X7) => any ? (x1: X1, x2: X2, x3: X3, x4: X4, x5: X5, x6: X6, x7: X7) => R : AnyFunc<R>; | ||
export declare type OverwriteReturn<F extends Function, R> = F extends ((...x: infer T) => unknown) ? ((...x: T) => R) : never; | ||
/** | ||
@@ -29,2 +29,2 @@ * Returns a tuple type of a functions arguments up to 7. | ||
*/ | ||
export declare type ArgsAsTuple<F extends AnyFunc> = F extends () => any ? void[] : F extends (x1: infer X1) => any ? [X1] : F extends (x1: infer X1, x2: infer X2) => any ? [X1, X2] : F extends (x1: infer X1, x2: infer X2, x3: infer X3) => any ? [X1, X2, X3] : F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4) => any ? [X1, X2, X3, X4] : F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5) => any ? [X1, X2, X3, X4, X5] : F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5, x6: infer X6) => any ? [X1, X2, X3, X4, X5, X6] : F extends (x1: infer X1, x2: infer X2, x3: infer X3, x4: infer X4, x5: infer X5, x6: infer X6, x7: infer X7) => any ? [X1, X2, X3, X4, X5, X6, X7] : any[]; | ||
export declare type ArgsAsTuple<F extends Function> = F extends ((...x: infer T) => unknown) ? T : never; |
@@ -1,4 +0,2 @@ | ||
import { Diff } from './strings'; | ||
import { False, True } from './conditionals'; | ||
import { AnyFunc } from './functions'; | ||
/** | ||
@@ -15,3 +13,3 @@ * An object with string keys and values of type `any`. | ||
export declare type ObjectType<T> = { | ||
[k in Keys<T>]: T[k]; | ||
[k in keyof T]: T[k]; | ||
}; | ||
@@ -35,3 +33,3 @@ /** | ||
*/ | ||
export declare type GetKey<T, K extends keyof any> = K extends Keys<T> ? T[K] : never; | ||
export declare type GetKey<T, K extends keyof any> = K extends keyof T ? T[K] : never; | ||
/** | ||
@@ -53,10 +51,4 @@ * Objects can be indexed by multiple types: `string`, `number`, `symbol`. | ||
*/ | ||
export declare type StringKeys<T> = Exclude<Keys<T>, number | symbol>; | ||
export declare type StringKeys<T> = Exclude<keyof T, number | symbol>; | ||
/** | ||
* No different than `keyof`, but can look a bit nicer when nesting many types deep. | ||
* @param T type from which to get keys | ||
* @returns keys of `T` that extend `string | number | symbol` | ||
*/ | ||
export declare type Keys<T> = keyof T; | ||
/** | ||
* When an object has optional or readonly keys, that information is contained within the key. | ||
@@ -68,3 +60,3 @@ * When using optional/readonly keys in another object, they will retain optional/readonly status. | ||
*/ | ||
export declare type PureKeys<T> = Record<Keys<T>, Keys<T>>[Keys<T>]; | ||
export declare type PureKeys<T> = Record<keyof T, keyof T>[keyof T]; | ||
/** | ||
@@ -76,3 +68,3 @@ * Gets all of the keys that are shared between two objects. | ||
*/ | ||
export declare type SharedKeys<T, U> = Keys<T> & Keys<U>; | ||
export declare type SharedKeys<T, U> = keyof T & keyof U; | ||
/** | ||
@@ -84,6 +76,6 @@ * Gets all keys between two objects. | ||
*/ | ||
export declare type AllKeys<T, U> = Keys<T> | Keys<U>; | ||
export declare type AllKeys<T, U> = keyof T | keyof U; | ||
/** | ||
* Gets all of the keys that are different between two objects. | ||
* This is a set difference between `Keys<T>` and `Keys<U>`. | ||
* This is a set difference between `keyof T` and `keyof U`. | ||
* Note that calling this with arguments reversed will have different results. | ||
@@ -94,3 +86,3 @@ * @param T first type from which keys will be pulled | ||
*/ | ||
export declare type DiffKeys<T, U> = Diff<Keys<T>, Keys<U>>; | ||
export declare type DiffKeys<T, U> = Exclude<keyof T, keyof U>; | ||
/** | ||
@@ -102,3 +94,3 @@ * Returns `True` if a key, `K`, is present in a type, `T`, else `False`. | ||
*/ | ||
export declare type HasKey<T, K extends keyof any> = K extends Keys<T> ? True : False; | ||
export declare type HasKey<T, K extends keyof any> = K extends keyof T ? True : False; | ||
/** | ||
@@ -108,3 +100,3 @@ * @param T the union to get the keys of | ||
*/ | ||
export declare type UnionKeys<T> = T extends any ? keyof T : never; | ||
export declare type UnionKeys<T> = T extends unknown ? keyof T : never; | ||
/** | ||
@@ -115,3 +107,3 @@ * Get a union of the properties of an object. | ||
*/ | ||
export declare type UnionizeProperties<T extends object> = T[Keys<T>]; | ||
export declare type UnionizeProperties<T extends object> = T[keyof T]; | ||
/** | ||
@@ -124,3 +116,3 @@ * Gives back an object with listed keys removed. | ||
*/ | ||
export declare type Omit<T extends object, K extends Keys<T>> = Pick<T, Diff<Keys<T>, K>>; | ||
export declare type Omit<T extends object, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; | ||
/** | ||
@@ -159,3 +151,3 @@ * Returns only the shared properties between two objects. | ||
export declare type TaggedObject<T extends Record<keyof any, object>, Key extends keyof any> = { | ||
[K in Keys<T>]: T[K] & Record<Key, K>; | ||
[K in keyof T]: T[K] & Record<Key, K>; | ||
}; | ||
@@ -169,3 +161,3 @@ /** | ||
export declare type DeepPartial<T> = Partial<{ | ||
[k in Keys<T>]: T[k] extends any[] ? Array<DeepPartial<T[k][number]>> : T[k] extends AnyFunc ? T[k] : T[k] extends object ? DeepPartial<T[k]> : T[k]; | ||
[k in keyof T]: T[k] extends unknown[] ? Array<DeepPartial<T[k][number]>> : T[k] extends Function ? T[k] : T[k] extends object ? DeepPartial<T[k]> : T[k]; | ||
}>; | ||
@@ -178,3 +170,3 @@ /** | ||
export declare type AllRequired<T extends object> = { | ||
[K in Keys<T>]-?: NonNullable<T[K]>; | ||
[K in keyof T]-?: NonNullable<T[K]>; | ||
}; | ||
@@ -187,3 +179,3 @@ /** | ||
*/ | ||
export declare type Required<T extends object, K extends Keys<T>> = CombineObjects<{ | ||
export declare type Required<T extends object, K extends keyof T> = CombineObjects<{ | ||
[k in K]-?: NonNullable<T[k]>; | ||
@@ -197,3 +189,3 @@ }, Omit<T, K>>; | ||
*/ | ||
export declare type Optional<T extends object, K extends Keys<T>> = CombineObjects<{ | ||
export declare type Optional<T extends object, K extends keyof T> = CombineObjects<{ | ||
[k in K]?: T[k] | undefined; | ||
@@ -207,3 +199,3 @@ }, Omit<T, K>>; | ||
export declare type DeepReadonly<T> = Readonly<{ | ||
[k in Keys<T>]: T[k] extends any[] ? ReadonlyArray<DeepReadonly<T[k][number]>> : T[k] extends AnyFunc ? T[k] : T[k] extends object ? DeepReadonly<T[k]> : T[k]; | ||
[k in keyof T]: T[k] extends unknown[] ? ReadonlyArray<DeepReadonly<T[k][number]>> : T[k] extends Function ? T[k] : T[k] extends object ? DeepReadonly<T[k]> : T[k]; | ||
}>; | ||
@@ -237,2 +229,2 @@ /** | ||
/** no-doc */ | ||
export declare type _StrictUnionHelper<UnionMember, Union> = UnionMember extends any ? UnionMember & Partial<Record<Exclude<UnionKeys<Union>, keyof UnionMember>, never>> : never; | ||
export declare type _StrictUnionHelper<UnionMember, Union> = UnionMember extends unknown ? UnionMember & Partial<Record<Exclude<UnionKeys<Union>, keyof UnionMember>, never>> : never; |
import { False, True, And, Or, Not } from './conditionals'; | ||
import { Keys } from './objects'; | ||
import { AnyFunc } from './functions'; | ||
/** no-doc */ | ||
export declare type KnownProblemPrototypeKeys = 'toString' | 'toLocaleString' | 'hasOwnProperty' | 'isPrototypeOf' | 'propertyIsEnumerable' | 'constructor' | 'valueOf'; | ||
/** no-doc */ | ||
export declare type ArrayPrototypeKeys = Keys<any[]>; | ||
export declare type ArrayPrototypeKeys = keyof unknown[]; | ||
/** no-doc */ | ||
export declare type NumberPrototypeKeys = Keys<number>; | ||
export declare type NumberPrototypeKeys = keyof number; | ||
/** no-doc */ | ||
export declare type BooleanPrototypeKeys = Keys<false>; | ||
export declare type BooleanPrototypeKeys = keyof false; | ||
/** no-doc */ | ||
export declare type StringPrototypeKeys = Keys<string>; | ||
export declare type StringPrototypeKeys = keyof string; | ||
/** no-doc */ | ||
export declare type ObjectPrototypeKeys = Keys<Object>; | ||
export declare type ObjectPrototypeKeys = keyof Object; | ||
/** no-doc */ | ||
export declare type FunctionPrototypeKeys = Keys<Function>; | ||
export declare type FunctionPrototypeKeys = keyof Function; | ||
export declare type IsNever<S extends string> = Not<(Record<S, True> & Record<string, False>)[S]>; | ||
export declare type IsType<T, X> = X extends T ? True : False; | ||
export declare type IsArray<T> = T extends any[] ? True : False; | ||
export declare type IsArray<T> = T extends unknown[] ? True : False; | ||
export declare type IsNumber<T> = T extends number ? True : False; | ||
export declare type IsString<T> = T extends string ? True : False; | ||
export declare type IsFunction<T> = Or<T extends AnyFunc ? True : False, T extends Function ? True : False>; | ||
export declare type IsFunction<T> = Or<T extends Function ? True : False, T extends Function ? True : False>; | ||
export declare type IsStringFunction<T extends string> = And<IsString<T>, IsNever<T>>; | ||
@@ -25,0 +23,0 @@ export declare type IsBoolean<T> = T extends boolean ? True : False; |
import { True, False, And } from './conditionals'; | ||
import { IsNever } from './predicates'; | ||
export declare type Diff<T, U> = Exclude<T, U>; | ||
export declare type DropString<T extends string, U extends T> = Diff<T, U>; | ||
export declare type StringEqual<T extends string, U extends string> = And<IsNever<Diff<T, U>>, IsNever<Diff<U, T>>>; | ||
export declare type DropString<T extends string, U extends T> = Exclude<T, U>; | ||
export declare type StringEqual<T extends string, U extends string> = And<IsNever<Exclude<T, U>>, IsNever<Exclude<U, T>>>; | ||
export declare type UnionContains<T, U> = U extends T ? True : False; |
@@ -17,7 +17,2 @@ import { ObjectType } from "./objects"; | ||
/** | ||
* A constant type that mimics an unknowable type. | ||
* @returns `{} | null | undefined` | ||
*/ | ||
export declare type Unknown = {} | null | undefined; | ||
/** | ||
* no-doc - This is a helper for `Nominal` and is not useful on its own | ||
@@ -24,0 +19,0 @@ */ |
50639
518
1051