utility-types
Advanced tools
Comparing version 3.7.0 to 3.8.0
@@ -7,3 +7,4 @@ /** | ||
export { $Call, $Diff, $ElementType, $Keys, $NonMaybeType, $PropertyType, $ReadOnly, $Shape, $Values, Class, } from './utility-types'; | ||
export { Assign, Brand, DeepNonNullable, DeepPartial, DeepReadonly, DeepRequired, Diff, Falsey, FunctionKeys, Intersection, NonFunctionKeys, NonUndefined, Omit, OmitByValue, OmitByValueExact, OptionalKeys, Overwrite, Optional, PickByValue, PickByValueExact, Primitive, PromiseType, ReadonlyKeys, RequiredKeys, SetComplement, SetDifference, SetIntersection, Subtract, SymmetricDifference, Unionize, WritableKeys, } from './mapped-types'; | ||
export { Assign, Brand, DeepNonNullable, DeepPartial, DeepReadonly, DeepRequired, Diff, Falsey, FunctionKeys, Intersection, NonFunctionKeys, NonUndefined, Omit, OmitByValue, OmitByValueExact, OptionalKeys, Overwrite, Optional, PickByValue, PickByValueExact, Primitive, PromiseType, ReadonlyKeys, RequiredKeys, SetComplement, SetDifference, SetIntersection, Subtract, SymmetricDifference, Unionize, ValuesType, WritableKeys, AugmentedRequired as Required, } from './mapped-types'; | ||
export * from './type-guards'; | ||
export { getReturnOfExpression } from './functional-helpers'; |
@@ -7,3 +7,7 @@ "use strict"; | ||
*/ | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./type-guards")); | ||
// deprecated | ||
@@ -10,0 +14,0 @@ var functional_helpers_1 = require("./functional-helpers"); |
@@ -7,10 +7,10 @@ /** | ||
* Primitive | ||
* @desc Type representing primitive types in TypeScript: `number | boolean | string | symbol` | ||
* @desc Type representing primitive types in TypeScript: `number | bigint | boolean | string | symbol` | ||
* @example | ||
* type Various = number | boolean | string | symbol | object; | ||
* type Various = number | string | object; | ||
* | ||
* // Expect: object | ||
* Exclude<Various, Primitive> | ||
* type Cleaned = Exclude<Various, Primitive> | ||
*/ | ||
export declare type Primitive = number | boolean | string | symbol; | ||
export declare type Primitive = number | bigint | boolean | string | symbol; | ||
/** | ||
@@ -83,9 +83,9 @@ * Falsey | ||
* @example | ||
* type MixedProps = { name: string; setName: (name: string) => void }; | ||
* type MixedProps = {name: string; setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any;}; | ||
* | ||
* // Expect: "setName" | ||
* // Expect: "setName | someFn" | ||
* type Keys = FunctionKeys<MixedProps>; | ||
*/ | ||
export declare type FunctionKeys<T extends object> = { | ||
[K in keyof T]: T[K] extends Function ? K : never; | ||
[K in keyof T]-?: NonUndefined<T[K]> extends Function ? K : never; | ||
}[keyof T]; | ||
@@ -96,9 +96,9 @@ /** | ||
* @example | ||
* type MixedProps = { name: string; setName: (name: string) => void }; | ||
* type MixedProps = {name: string; setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any;}; | ||
* | ||
* // Expect: "name" | ||
* // Expect: "name | someKey" | ||
* type Keys = NonFunctionKeys<MixedProps>; | ||
*/ | ||
export declare type NonFunctionKeys<T extends object> = { | ||
[K in keyof T]: T[K] extends Function ? never : K; | ||
[K in keyof T]-?: NonUndefined<T[K]> extends Function ? never : K; | ||
}[keyof T]; | ||
@@ -260,3 +260,3 @@ /** | ||
* Subtract | ||
* @desc From `T` remove properties that exist in `T1` (`T1` is a subtype of `T`) | ||
* @desc From `T` remove properties that exist in `T1` (`T1` has a subset of the properties of `T`) | ||
* @example | ||
@@ -436,3 +436,3 @@ * type Props = { name: string; age: number; visible: boolean }; | ||
* Brand | ||
* @desc Define nominal type of U based on type of T. | ||
* @desc Define nominal type of U based on type of T. Similar to Opaque types in Flow. | ||
* @example | ||
@@ -475,2 +475,48 @@ * type USD = Brand<number, "USD"> | ||
export declare type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>; | ||
/** | ||
* ValuesType | ||
* @desc get the union type of all the values in an object, array or array-like type `T` | ||
* @example | ||
* type Props = { name: string; age: number; visible: boolean }; | ||
* // Expect: string | number | boolean | ||
* type PropsValues = ValuesType<Props>; | ||
* | ||
* type NumberArray = number[]; | ||
* // Expect: number | ||
* type NumberItems = ValuesType<NumberArray>; | ||
* | ||
* type ReadonlySymbolArray = readonly symbol[]; | ||
* // Expect: symbol | ||
* type SymbolItems = ValuesType<ReadonlySymbolArray>; | ||
* | ||
* type NumberTuple = [1, 2]; | ||
* // Expect: 1 | 2 | ||
* type NumberUnion = ValuesType<NumberTuple>; | ||
* | ||
* type ReadonlyNumberTuple = readonly [1, 2]; | ||
* // Expect: 1 | 2 | ||
* type AnotherNumberUnion = ValuesType<NumberTuple>; | ||
* | ||
* type BinaryArray = Uint8Array; | ||
* // Expect: number | ||
* type BinaryItems = ValuesType<BinaryArray>; | ||
*/ | ||
export declare type ValuesType<T extends ReadonlyArray<any> | ArrayLike<any> | Record<any, any>> = T extends ReadonlyArray<any> ? T[number] : T extends ArrayLike<any> ? T[number] : T extends object ? T[keyof T] : never; | ||
/** | ||
* Required | ||
* @desc From `T` make a set of properties by key `K` become required | ||
* @example | ||
* type Props = { | ||
* name?: string; | ||
* age?: number; | ||
* visible?: boolean; | ||
* }; | ||
* | ||
* // Expect: { name: string; age: number; visible: boolean; } | ||
* type Props = Required<Props>; | ||
* | ||
* // Expect: { name?: string; age: number; visible: boolean; } | ||
* type Props = Required<Props, 'age' | 'visible'>; | ||
*/ | ||
export declare type AugmentedRequired<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Required<Pick<T, K>>; | ||
export {}; |
{ | ||
"name": "utility-types", | ||
"version": "3.7.0", | ||
"version": "3.8.0", | ||
"description": "Utility Types Collection for TypeScript", | ||
@@ -16,17 +16,16 @@ "author": "Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io)", | ||
"hooks": { | ||
"pre-push": "npm run pre-push" | ||
"pre-push": "npm run prettier:fix && npm run lint && npm run tsc && npm run test:update" | ||
} | ||
}, | ||
"scripts": { | ||
"pre-push": "npm run prettier:fix && npm run lint && npm run tsc && npm run test:update", | ||
"ci-check": "npm run prettier && npm run lint && npm run tsc && npm run test", | ||
"reinstall": "rm -rf node_modules/ dist/ && npm install", | ||
"prettier": "prettier --list-different 'src/**/*.ts' || (echo '\nPlease fix code formatting by running:\nnpm run prettier:fix\n'; exit 1)", | ||
"prettier:fix": "prettier --write 'src/**/*.ts'", | ||
"lint": "tslint --project './tsconfig.json'", | ||
"prettier:fix": "prettier --write src/**/*.ts", | ||
"lint": "tslint --project ./tsconfig.json", | ||
"tsc": "tsc -p . --noEmit", | ||
"tsc:watch": "tsc -p . --noEmit -w", | ||
"test": "jest --config jest.config.json", | ||
"test": "jest --config jest.config.json && dts-jest-remap ./src/*.spec.ts --rename {{basename}}.snap.{{extname}} --check", | ||
"test:update": "jest --config jest.config.json --no-cache -u && dts-jest-remap ./src/*.spec.ts --rename {{basename}}.snap.{{extname}}", | ||
"test:watch": "jest --config jest.config.json --watch", | ||
"test:update": "jest --config jest.config.json --no-cache -u && dts-jest-remap ./src/*.spec.ts --rename '{{basename}}.snap.{{extname}}'", | ||
"prebuild": "rm -rf dist/", | ||
@@ -33,0 +32,0 @@ "build": "tsc -p ./tsconfig.build.json --outDir dist/", |
@@ -67,2 +67,7 @@ # utility-types | ||
## Type Guards | ||
* ['isPrimitive'](#isprimitive) | ||
* ['isFalsey'](#isfalsey) | ||
## Union operators | ||
@@ -86,7 +91,7 @@ | ||
* [`RequiredKeys<T>`](#requiredkeyst) | ||
* [`Optional<T>`](#optionalst) | ||
* [`Optional<T, K>`](#optionalt-k) | ||
* [`OptionalKeys<T>`](#optionalkeyst) | ||
* [`Partial<T>`](#partialt) _(built-in)_ | ||
* [`DeepPartial<T>`](#deeppartialt) | ||
* [`Required<T>`](#requiredt) _(built-in)_ | ||
* [`Required<T, K>`](#requiredt) | ||
* [`DeepRequired<T>`](#deeprequiredt) | ||
@@ -96,5 +101,7 @@ * [`Readonly<T>`](#readonlyt) _(built-in)_ | ||
* [`Pick<T, K>` _(built-in)_](#pickt-k-built-in) | ||
* [`Omit<T, K>`](#omitt-k) | ||
* [`Omit<T, K>`](#omitt-k) _(built-in)_ | ||
* [`PickByValue<T, ValueType>`](#pickbyvaluet-valuetype) | ||
* [`PickByValueExact<T, ValueType>`](#pickbyvalueexactt-valuetype) | ||
* [`OmitByValue<T, ValueType>`](#omitbyvaluet-valuetype) | ||
* [`OmitByValueExact<T, ValueType>`](#omitbyvalueexactt-valuetype) | ||
* [`Intersection<T, U>`](#intersectiont-u) | ||
@@ -105,2 +112,3 @@ * [`Diff<T, U>`](#difft-u) | ||
* [`Assign<T, U>`](#assignt-u) | ||
* [`ValuesType<T>`](#valuestypet) | ||
@@ -136,4 +144,6 @@ ## Special operators | ||
Type representing primitive types in TypeScript: `number | boolean | string | symbol` | ||
Type representing primitive types in JavaScript, and thus TypeScript: `number | bigint | boolean | string | symbol` | ||
You can test for singular of these types with [`typeof`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) | ||
[⇧ back to top](#table-of-contents) | ||
@@ -148,2 +158,26 @@ | ||
### `isPrimitive` | ||
This is a [TypeScript Typeguard](https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) for the [`Primitive`](#primitive) type. | ||
This can be useful to control the type of a parameter as the program flows. Example: | ||
```ts | ||
const consumer = (param: Primitive[] | Primitive): string => { | ||
if (isPrimitive(param)) { | ||
// typeof param === Primitive | ||
return String(param) + ' was Primitive'; | ||
} | ||
// typeof param === Primitive[] | ||
const resultArray = param | ||
.map(consumer) | ||
.map(rootString => '\n\t' + rootString); | ||
return resultArray.reduce((comm, newV) => comm + newV, 'this was nested:'); | ||
}; | ||
``` | ||
### `isFalsey` | ||
As `isPrimitive` but for the type [`Falsey`](#falsey). | ||
### `SetIntersection<A, B>` (same as Extract) | ||
@@ -510,3 +544,3 @@ | ||
From `T` remove properties that exist in `T1` (`T1` is a subtype of `T`) | ||
From `T` remove properties that exist in `T1` (`T1` has a subset of the properties of `T`) | ||
@@ -563,2 +597,30 @@ **Usage:** | ||
### `ValuesType<T>` | ||
Get the union type of all the values in an object, tuple, array or array-like type `T`. | ||
**Usage:** | ||
```ts | ||
import { ValuesType } from 'utility-types'; | ||
type Props = { name: string; age: number; visible: boolean }; | ||
// Expect: string | number | boolean | ||
type PropsValues = $ValuesType<Props>; | ||
type NumberArray = number[]; | ||
// Expect: number | ||
type NumberItems = $ValuesType<NumberArray>; | ||
type ReadonlyNumberTuple = readonly [1, 2]; | ||
// Expect: 1 | 2 | ||
type AnotherNumberUnion = $ValuesType<NumberTuple>; | ||
type BinaryArray = Uint8Array; | ||
// Expect: number | ||
type BinaryItems = $ValuesType<BinaryArray>; | ||
``` | ||
[⇧ back to top](#table-of-contents) | ||
### `Partial<T>` | ||
@@ -570,6 +632,19 @@ | ||
### `Required<T>` | ||
### `Required<T, K>` | ||
Make all properties of object type non-optional | ||
From `T` make a set of properties by key `K` become required | ||
**Usage:** | ||
```ts | ||
import { Required } from 'utility-types'; | ||
type Props = { name?: string; age?: number; visible?: boolean; }; | ||
// Expect: { name: string; age: number; visible: boolean; } | ||
type Props = Required<Props> | ||
// Expect: { name?: string; age: number; visible: boolean; } | ||
type Props = Required<Props, 'age' | 'visible'>; | ||
``` | ||
[⇧ back to top](#table-of-contents) | ||
@@ -745,3 +820,3 @@ | ||
Define nominal type of `U` based on type of `T`. | ||
Define nominal type of `U` based on type of `T`. Similar to Opaque types in Flow. | ||
@@ -992,4 +1067,11 @@ **Usage:** | ||
## Related Projects | ||
- [`ts-toolbelt`](https://github.com/pirix-gh/ts-toolbelt) - Higher type safety for TypeScript | ||
- [`$mol_type`](https://github.com/eigenmethod/mol/tree/master/type) - Collection of TypeScript meta types for complex logic | ||
--- | ||
MIT License | ||
Copyright (c) 2016 Piotr Witek <mailto:piotrek.witek@gmail.com> (http://piotrwitek.github.io) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58833
18
809
1067