Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3dfb58e
commit 28199b0
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
Create a type that requires all of the given keys or none of the given keys. The remaining keys are kept as is. | ||
Use-cases: | ||
- Creating interfaces for components with mutually-inclusive keys. | ||
The caveat with `RequireAllOrNone` is that TypeScript doesn't always know at compile time every key that will exist at runtime. Therefore `RequireAllOrNone` can't do anything to prevent extra keys it doesn't know about. | ||
@example | ||
``` | ||
import {RequireAllOrNone} from 'type-fest'; | ||
type Responder = { | ||
text?: () => string; | ||
json?: () => string; | ||
secure: boolean; | ||
}; | ||
const responder1: RequireAllOrNone<Responder, 'text' | 'json'> = { | ||
secure: true | ||
}; | ||
const responder2: RequireAllOrNone<Responder, 'text' | 'json'> = { | ||
text: () => '{"message": "hi"}', | ||
json: () => '{"message": "ok"}', | ||
secure: true | ||
}; | ||
``` | ||
@category Utilities | ||
*/ | ||
export type RequireAllOrNone<ObjectType, KeysType extends keyof ObjectType = never> = ( | ||
| Required<Pick<ObjectType, KeysType>> // Require all of the given keys. | ||
| Partial<Record<KeysType, never>> // Require none of the given keys. | ||
) & | ||
Omit<ObjectType, KeysType>; // The rest of the keys. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {expectError, expectAssignable} from 'tsd'; | ||
import {RequireAllOrNone} from '../index'; | ||
|
||
type SystemMessages = { | ||
default: string; | ||
|
||
macos: string; | ||
linux: string; | ||
|
||
optional?: string; | ||
}; | ||
|
||
type ValidMessages = RequireAllOrNone<SystemMessages, 'macos' | 'linux'>; | ||
const test = (_: ValidMessages): void => {}; // eslint-disable-line @typescript-eslint/no-empty-function | ||
|
||
test({default: 'hello'}); | ||
test({macos: 'yo', linux: 'sup', optional: 'howdy', default: 'hello'}); | ||
|
||
expectError(test({})); | ||
expectError(test({macos: 'hey', default: 'hello'})); | ||
expectError(test({linux: 'hey', default: 'hello'})); | ||
|
||
declare const oneWithoutKeys: RequireAllOrNone<{a: number; b: number}>; | ||
expectAssignable<{a: number; b: number}>(oneWithoutKeys); |