-
-
Notifications
You must be signed in to change notification settings - Fork 571
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
80465bc
commit 0a9d935
Showing
4 changed files
with
58 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,32 @@ | ||
/** | ||
Create a type that requires exactly one of the given properties and disallows more. The remaining properties are kept as is. | ||
Use-cases: | ||
- Creating interfaces for components that only need one of the properties to display properly. | ||
- Declaring generic properties in a single place for a single use-case that gets narrowed down via `RequireExactlyOne`. | ||
The caveat with `RequireExactlyOne` is that TypeScript doesn't always know at compile time every property that will exist at runtime. Therefore `RequireExactlyOne` can't do anything to prevent extra properties it doesn't know about. | ||
@example | ||
``` | ||
import {RequireExactlyOne} from 'type-fest'; | ||
type Responder = { | ||
text: () => string; | ||
json: () => string; | ||
secure: boolean; | ||
}; | ||
const responder: RequireExactlyOne<Responder, 'text' | 'json'> = { | ||
// Adding a `text` property here would cause a compile error. | ||
json: () => '{"message": "ok"}', | ||
secure: true | ||
}; | ||
``` | ||
*/ | ||
export type RequireExactlyOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = | ||
{[Key in KeysType]: ( | ||
Required<Pick<ObjectType, Key>> & | ||
Partial<Record<Exclude<KeysType, Key>, never>> | ||
)}[KeysType] & Omit<ObjectType, KeysType>; |
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 {expectType, expectError} from 'tsd'; | ||
import {RequireExactlyOne} from '..'; | ||
|
||
type SystemMessages = { | ||
default: string; | ||
|
||
macos: string; | ||
linux: string; | ||
|
||
optional?: string; | ||
}; | ||
|
||
type ValidMessages = RequireExactlyOne<SystemMessages, 'macos' | 'linux'>; | ||
const test = (_: ValidMessages): void => {}; | ||
|
||
test({macos: 'hey', default: 'hello'}); | ||
test({linux: 'sup', optional: 'howdy', default: 'hello'}); | ||
|
||
expectError(test({})); | ||
expectError(test({macos: 'hey', linux: 'sup', default: 'hello'})); | ||
|
||
declare const oneWithoutKeys: RequireExactlyOne<{a: number; b: number}>; | ||
expectType<{a: number} | {b: number}>(oneWithoutKeys); | ||
expectError(expectType<{a: number; b: number}>(oneWithoutKeys)); |