Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ConditionalKeys
, ConditionalPick
and ConditionalExcept
types
- Loading branch information
Showing
8 changed files
with
209 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,45 @@ | ||
import {Except} from './except'; | ||
import {ConditionalKeys} from './conditional-keys'; | ||
|
||
/** | ||
Exclude the keys from any shape which matches the provided `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; } | ||
``` | ||
A simpler and more contrived example is below. | ||
@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 provided 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 `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 provided base type. | ||
[Key in keyof Base]: | ||
// Pick only keys with types extending the `Condition` type provided. | ||
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 pass 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,44 @@ | ||
import {ConditionalKeys} from './conditional-keys'; | ||
|
||
/** | ||
Pick all keys from them shape which match the provided `Condition`. | ||
This is useful when you want to create a new type from a specific subset from an already created 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; } | ||
``` | ||
A simpler and more contrived example is below. | ||
@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); |