type-fest
Advanced tools
Comparing version 0.3.1 to 0.4.0
215
index.d.ts
@@ -1,206 +0,13 @@ | ||
export {PackageJson} from './source/package-json'; | ||
// Basic | ||
export * from './source/basic'; | ||
// TODO: Add more examples | ||
// Utilities | ||
export {Omit} from './source/omit'; | ||
export {Mutable} from './source/mutable'; | ||
export {Merge} from './source/merge'; | ||
export {MergeExclusive} from './source/merge-exclusive'; | ||
export {RequireAtLeastOne} from './source/require-at-least-one'; | ||
export {LiteralUnion} from './source/literal-union'; | ||
// TODO: This can just be `export type Primitive = not object` when the `not` keyword is out. | ||
/** | ||
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive). | ||
*/ | ||
export type Primitive = | ||
| null | ||
| undefined | ||
| string | ||
| number | ||
| boolean | ||
| symbol; | ||
/** | ||
Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes). | ||
*/ | ||
export type Class<T = unknown> = new(...arguments_: any[]) => T; | ||
/** | ||
Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`. | ||
*/ | ||
export type TypedArray = | ||
| Int8Array | ||
| Uint8Array | ||
| Uint8ClampedArray | ||
| Int16Array | ||
| Uint16Array | ||
| Int32Array | ||
| Uint32Array | ||
| Float32Array | ||
| Float64Array; | ||
/** | ||
Matches a JSON object. | ||
*/ | ||
export type JsonObject = {[key: string]: JsonValue}; | ||
/** | ||
Matches a JSON array. | ||
*/ | ||
export interface JsonArray extends Array<JsonValue> {} | ||
/** | ||
Matches any valid JSON value. | ||
*/ | ||
export type JsonValue = string | number | boolean | null | JsonObject | JsonArray; | ||
declare global { | ||
interface SymbolConstructor { | ||
readonly observable: symbol; | ||
} | ||
} | ||
/** | ||
Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable). | ||
*/ | ||
export interface ObservableLike { | ||
subscribe(observer: (value: unknown) => void): void; | ||
[Symbol.observable](): ObservableLike; | ||
} | ||
/** | ||
Create a type from an object type without certain keys. | ||
@example | ||
``` | ||
import {Omit} from 'type-fest'; | ||
type Foo = { | ||
a: number; | ||
b: string; | ||
}; | ||
type FooWithoutA = Omit<Foo, 'a'>; | ||
//=> {b: string}; | ||
``` | ||
I'm surprised this one is not built-in. It seems [other people agree](https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-420919470). Please 👍 [this issue on TypeScript](https://github.com/Microsoft/TypeScript/issues/30455) about making it built-in. | ||
*/ | ||
export type Omit<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>; | ||
/** | ||
Merge two types into a new type. Keys of the second type overrides keys of the first type. | ||
@example | ||
``` | ||
import {Merge} from 'type-fest'; | ||
type Foo = { | ||
a: number; | ||
b: string; | ||
}; | ||
type Bar = { | ||
b: number; | ||
}; | ||
const ab: Merge<Foo, Bar> = {a: 1, b: 2}; | ||
``` | ||
*/ | ||
export type Merge<FirstType, SecondType> = Omit<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType; | ||
// Helper type. Not useful on its own. | ||
type Without<FirstType, SecondType> = {[KeyType in Exclude<keyof FirstType, keyof SecondType>]?: never}; | ||
/** | ||
Create a type that has mutually exclusive properties. | ||
This type was inspired by [this comment](https://github.com/Microsoft/TypeScript/issues/14094#issuecomment-373782604). | ||
This type works with a helper type, called `Without`. `Without<FirstType, SecondType>` produces a type that has only keys from `FirstType` which are not present on `SecondType` and sets the value type for these keys to `never`. This helper type is then used in `MergeExclusive` to remove keys from either `FirstType` or `SecondType`. | ||
@example | ||
``` | ||
import {MergeExclusive} from 'type-fest'; | ||
interface ExclusiveVariation1 { | ||
exclusive1: boolean; | ||
} | ||
interface ExclusiveVariation2 { | ||
exclusive2: string; | ||
} | ||
type ExclusiveOptions = MergeExclusive<ExclusiveVariation1, ExclusiveVariation2>; | ||
let exclusiveOptions: ExclusiveOptions; | ||
exclusiveOptions = {exclusive1: true}; | ||
//=> Works | ||
exclusiveOptions = {exclusive2: 'hi'}; | ||
//=> Works | ||
exclusiveOptions = {exclusive1: true, exclusive2: 'hi'}; | ||
//=> Error | ||
``` | ||
*/ | ||
export type MergeExclusive<FirstType, SecondType> = | ||
(FirstType | SecondType) extends object ? | ||
(Without<FirstType, SecondType> & SecondType) | (Without<SecondType, FirstType> & FirstType) : | ||
FirstType | SecondType; | ||
/** | ||
Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. | ||
Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals. | ||
This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore. | ||
@example | ||
``` | ||
import {LiteralUnion} from 'type-fest'; | ||
// Before | ||
type Pet = 'dog' | 'cat' | string; | ||
const pet: Pet = ''; | ||
// Start typing in your TypeScript-enabled IDE. | ||
// You **will not** get auto-completion for `dog` and `cat` literals. | ||
// After | ||
type Pet2 = LiteralUnion<'dog' | 'cat', string>; | ||
const pet: Pet2 = ''; | ||
// You **will** get auto-completion for `dog` and `cat` literals. | ||
``` | ||
*/ | ||
export type LiteralUnion< | ||
LiteralType extends BaseType, | ||
BaseType extends Primitive | ||
> = LiteralType | (BaseType & {_?: never}); | ||
/* | ||
Create a type that requires at least one of the given properties. The remaining properties are kept as is. | ||
@example | ||
``` | ||
import {RequireAtLeastOne} from 'type-fest'; | ||
type Responder = { | ||
text?: () => string; | ||
json?: () => string; | ||
secure?: boolean; | ||
}; | ||
const responder: RequireAtLeastOne<Responder, 'text' | 'json'> = { | ||
json: () => '{"message": "ok"}', | ||
secure: true | ||
}; | ||
``` | ||
*/ | ||
export type RequireAtLeastOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = | ||
{ | ||
// For each Key in KeysType make a mapped type | ||
[Key in KeysType]: ( | ||
// …by picking that Key's type and making it required | ||
Required<Pick<ObjectType, Key>> | ||
) | ||
}[KeysType] | ||
// …then, make intersection types by adding the remaining properties to each mapped type. | ||
& Omit<ObjectType, KeysType>; | ||
// Miscellaneous | ||
export {PackageJson} from './source/package-json'; |
{ | ||
"name": "type-fest", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "A collection of essential TypeScript types", | ||
@@ -36,3 +36,3 @@ "license": "(MIT OR CC0-1.0)", | ||
"@typescript-eslint/eslint-plugin": "^1.5.0", | ||
"eslint-config-xo-typescript": "^0.9.0", | ||
"eslint-config-xo-typescript": "^0.10.0", | ||
"tsd": "^0.7.2", | ||
@@ -47,5 +47,6 @@ "xo": "^0.24.0" | ||
"rules": { | ||
"import/no-unresolved": "off" | ||
"import/no-unresolved": "off", | ||
"@typescript-eslint/indent": "off" | ||
} | ||
} | ||
} |
@@ -51,25 +51,26 @@ <div align="center"> | ||
See the [types file](index.d.ts) for complete docs. | ||
Click the type names for complete docs. | ||
### Basic | ||
- `Primitive` - Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive). | ||
- `Class` - Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes). | ||
- `TypedArray` - Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`. | ||
- `JsonObject` - Matches a JSON object. | ||
- `JsonArray` - Matches a JSON array. | ||
- `JsonValue` - Matches any valid JSON value. | ||
- `ObservableLike` - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable). | ||
- [`Primitive`](source/basic.d.ts) - Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive). | ||
- [`Class`](source/basic.d.ts) - Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes). | ||
- [`TypedArray`](source/basic.d.ts) - Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`. | ||
- [`JsonObject`](source/basic.d.ts) - Matches a JSON object. | ||
- [`JsonArray`](source/basic.d.ts) - Matches a JSON array. | ||
- [`JsonValue`](source/basic.d.ts) - Matches any valid JSON value. | ||
- [`ObservableLike`](source/basic.d.ts) - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable). | ||
### Utilities | ||
- `Omit` - Create a type from an object type without certain keys. | ||
- `Merge` - Merge two types into a new type. Keys of the second type overrides keys of the first type. | ||
- `MergeExclusive` - Create a type that has mutually exclusive properties. | ||
- `RequireAtLeastOne` - Create a type that requires at least one of the given properties. | ||
- `LiteralUnion` - Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). | ||
- [`Omit`](source/omit.d.ts) - Create a type from an object type without certain keys. | ||
- [`Mutable`](source/mutable.d.ts) - Convert an object with `readonly` properties into a mutable object. Inverse of `Readonly<T>`. | ||
- [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type. | ||
- [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive properties. | ||
- [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given properties. | ||
- [`LiteralUnion`](source/literal-union.d.ts) - Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). | ||
### Miscellaneous | ||
- `PackageJson` - Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). | ||
- [`PackageJson`](source/package-json.d.ts) - Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). | ||
@@ -76,0 +77,0 @@ |
@@ -310,6 +310,6 @@ import {LiteralUnion} from '..'; | ||
*/ | ||
licenses?: { | ||
licenses?: Array<{ | ||
type?: string; | ||
url?: string; | ||
}[]; | ||
}>; | ||
@@ -424,3 +424,3 @@ author?: PackageJson.Person; | ||
*/ | ||
os?: LiteralUnion< | ||
os?: Array<LiteralUnion< | ||
| 'aix' | ||
@@ -441,3 +441,3 @@ | 'darwin' | ||
string | ||
>[]; | ||
>>; | ||
@@ -447,3 +447,3 @@ /** | ||
*/ | ||
cpu?: LiteralUnion< | ||
cpu?: Array<LiteralUnion< | ||
| 'arm' | ||
@@ -472,3 +472,3 @@ | 'arm64' | ||
string | ||
>[]; | ||
>>; | ||
@@ -475,0 +475,0 @@ /** |
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
24462
12
606
114