Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add
ConditionalKeys
, ConditionalPick
and ConditionalExcept
types (
#76) Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
ae34d9e
commit 8a908a3
Showing
8 changed files
with
205 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,43 @@ | ||
import {Except} from './except'; | ||
import {ConditionalKeys} from './conditional-keys'; | ||
|
||
/** | ||
Exclude keys from a shape that matches the given `Condition`. | ||
This is useful when you want to create a new type with a specific set of keys from a shape. For example, you might want to exclude all the primitive properties from a class and form a new shape containing everything but the primitive properties. | ||
@example | ||
``` | ||
import {Primitive, ConditionalExcept} from 'type-fest'; | ||
class Awesome { | ||
name: string; | ||
successes: number; | ||
failures: bigint; | ||
run() {} | ||
} | ||
type ExceptPrimitivesFromAwesome = ConditionalExcept<Awesome, Primitive>; | ||
//=> {run: () => void} | ||
``` | ||
@example | ||
``` | ||
import {ConditionalExcept} from 'type-fest'; | ||
interface Example { | ||
a: string; | ||
b: string | number; | ||
c: () => void; | ||
d: {}; | ||
} | ||
type NonStringKeysOnly = ConditionalExcept<Example, string>; | ||
//=> {b: string | number; c: () => void; d: {}} | ||
``` | ||
*/ | ||
export type ConditionalExcept<Base, Condition> = Except< | ||
Base, | ||
ConditionalKeys<Base, Condition> | ||
>; |
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,43 @@ | ||
/** | ||
Extract the keys from a type where the value type of the key extends the given `Condition`. | ||
Internally this is used for the `ConditionalPick` and `ConditionalExcept` types. | ||
@example | ||
``` | ||
import {ConditionalKeys} from 'type-fest'; | ||
interface Example { | ||
a: string; | ||
b: string | number; | ||
c?: string; | ||
d: {}; | ||
} | ||
type StringKeysOnly = ConditionalKeys<Example, string>; | ||
//=> 'a' | ||
``` | ||
To support partial types, make sure your `Condition` is a union of undefined (for example, `string | undefined`) as demonstrated below. | ||
@example | ||
``` | ||
type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>; | ||
//=> 'a' | 'c' | ||
``` | ||
*/ | ||
export type ConditionalKeys<Base, Condition> = NonNullable< | ||
// Wrap in `NonNullable` to strip away the `undefined` type from the produced union. | ||
{ | ||
// Map through all the keys of the given base type. | ||
[Key in keyof Base]: | ||
// Pick only keys with types extending the given `Condition` type. | ||
Base[Key] extends Condition | ||
// Retain this key since the condition passes. | ||
? Key | ||
// Discard this key since the condition fails. | ||
: never; | ||
|
||
// Convert the produced object into a union type of the keys which passed the conditional test. | ||
}[keyof Base] | ||
>; |
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,42 @@ | ||
import {ConditionalKeys} from './conditional-keys'; | ||
|
||
/** | ||
Pick keys from the shape that matches the given `Condition`. | ||
This is useful when you want to create a new type from a specific subset of an existing type. For example, you might want to pick all the primitive properties from a class and form a new automatically derived type. | ||
@example | ||
``` | ||
import {Primitive, ConditionalPick} from 'type-fest'; | ||
class Awesome { | ||
name: string; | ||
successes: number; | ||
failures: bigint; | ||
run() {} | ||
} | ||
type PickPrimitivesFromAwesome = ConditionalPick<Awesome, Primitive>; | ||
//=> {name: string; successes: number; failures: bigint} | ||
``` | ||
@example | ||
``` | ||
import {ConditionalPick} from 'type-fest'; | ||
interface Example { | ||
a: string; | ||
b: string | number; | ||
c: () => void; | ||
d: {}; | ||
} | ||
type StringKeysOnly = ConditionalPick<Example, string>; | ||
//=> {a: string} | ||
``` | ||
*/ | ||
export type ConditionalPick<Base, Condition> = Pick< | ||
Base, | ||
ConditionalKeys<Base, Condition> | ||
>; |
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,28 @@ | ||
import {expectType} from 'tsd'; | ||
import {ConditionalExcept, Primitive} from '..'; | ||
|
||
class Awesome { | ||
name!: string; | ||
successes!: number; | ||
failures!: bigint; | ||
|
||
run(): void { | ||
// Empty | ||
} | ||
} | ||
|
||
interface Example { | ||
a: string; | ||
b?: string | number; | ||
c?: string; | ||
d: Record<string, unknown>; | ||
} | ||
|
||
declare const exampleConditionalExcept: ConditionalExcept<Example, string>; | ||
expectType<{b?: string | number; c?: string; d: Record<string, unknown>}>(exampleConditionalExcept); | ||
|
||
declare const awesomeConditionalExcept: ConditionalExcept<Awesome, Primitive>; | ||
expectType<{run: () => void}>(awesomeConditionalExcept); | ||
|
||
declare const exampleConditionalExceptWithUndefined: ConditionalExcept<Example, string | undefined>; | ||
expectType<{b?: string | number; d: Record<string, unknown>}>(exampleConditionalExceptWithUndefined); |
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,15 @@ | ||
import {expectType} from 'tsd'; | ||
import {ConditionalKeys} from '..'; | ||
|
||
interface Example { | ||
a: string; | ||
b?: string | number; | ||
c?: string; | ||
d: Record<string, unknown>; | ||
} | ||
|
||
declare const exampleConditionalKeys: ConditionalKeys<Example, string>; | ||
expectType<'a'>(exampleConditionalKeys); | ||
|
||
declare const exampleConditionalKeysWithUndefined: ConditionalKeys<Example, string | undefined>; | ||
expectType<'a' | 'c'>(exampleConditionalKeysWithUndefined); |
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,28 @@ | ||
import {expectType} from 'tsd'; | ||
import {ConditionalPick, Primitive} from '..'; | ||
|
||
class Awesome { | ||
name!: string; | ||
successes!: number; | ||
failures!: bigint; | ||
|
||
run(): void { | ||
// Empty | ||
} | ||
} | ||
|
||
interface Example { | ||
a: string; | ||
b?: string | number; | ||
c?: string; | ||
d: Record<string, unknown>; | ||
} | ||
|
||
declare const exampleConditionalPick: ConditionalPick<Example, string>; | ||
expectType< {a: string}>(exampleConditionalPick); | ||
|
||
declare const awesomeConditionalPick: ConditionalPick<Awesome, Primitive>; | ||
expectType<{name: string; successes: number; failures: bigint}>(awesomeConditionalPick); | ||
|
||
declare const exampleConditionalPickWithUndefined: ConditionalPick<Example, string | undefined>; | ||
expectType<{a: string; c?: string}>(exampleConditionalPickWithUndefined); |