type-fest
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -100,3 +100,42 @@ export {PackageJson} from './source/package-json'; | ||
// 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. | ||
@@ -132,1 +171,32 @@ | ||
> = 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>; |
{ | ||
"name": "type-fest", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A collection of essential TypeScript types", | ||
@@ -36,3 +36,3 @@ "license": "(MIT OR CC0-1.0)", | ||
"eslint-config-xo-typescript": "^0.8.0", | ||
"tsd-check": "^0.3.0", | ||
"tsd-check": "^0.5.0", | ||
"xo": "^0.24.0" | ||
@@ -39,0 +39,0 @@ }, |
@@ -67,2 +67,4 @@ <div align="center"> | ||
- `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). | ||
@@ -69,0 +71,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22744
573
106
0