-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types):
IsRequiredKey
, IfRequiredKey
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
bac20f5
commit 16e2738
Showing
6 changed files
with
103 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @file Type Tests - IfRequiredKey | ||
* @module tutils/types/tests/unit-d/IfRequiredKey | ||
*/ | ||
|
||
import type Author from '#fixtures/author.interface' | ||
import type TestSubject from '../if-key-required' | ||
|
||
describe('unit-d:types/IfRequiredKey', () => { | ||
type False = false | ||
type True = true | ||
|
||
it('should equal False if IsRequiredKey<T, K> extends false', () => { | ||
// Arrange | ||
type K = 'email' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<Author, K, True, False>>().toEqualTypeOf<False>() | ||
}) | ||
|
||
it('should equal True if IsRequiredKey<T, K> extends true', () => { | ||
// Arrange | ||
type K = 'first_name' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<Author, K, True, False>>().toEqualTypeOf<True>() | ||
}) | ||
}) |
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,25 @@ | ||
/** | ||
* @file Type Tests - IsRequiredKey | ||
* @module tutils/types/tests/unit-d/IsRequiredKey | ||
*/ | ||
|
||
import type Author from '#fixtures/author.interface' | ||
import type TestSubject from '../is-key-required' | ||
|
||
describe('unit-d:types/IsRequiredKey', () => { | ||
it('should equal false if K is not required property of T', () => { | ||
// Arrange | ||
type K = 'email' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<Author, K>>().toEqualTypeOf<false>() | ||
}) | ||
|
||
it('should equal true if K is required property of T', () => { | ||
// Arrange | ||
type K = 'last_name' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<Author, K>>().toEqualTypeOf<true>() | ||
}) | ||
}) |
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,27 @@ | ||
/** | ||
* @file Type Definitions - IfRequiredKey | ||
* @module tutils/types/IfRequiredKey | ||
*/ | ||
|
||
import type IndexSignature from './index-signature' | ||
import type IsRequiredKey from './is-key-required' | ||
|
||
/** | ||
* Conditional type that resolves depending on whether or not `K` is a required | ||
* property of `T`. | ||
* | ||
* @see {@linkcode IsRequiredKey} | ||
* | ||
* @template T - Type to evaluate | ||
* @template K - Key to evaluate | ||
* @template True - Type if `K` is required property | ||
* @template False - Type if `K` is not required property | ||
*/ | ||
type IfRequiredKey<T, K extends IndexSignature, True, False> = IsRequiredKey< | ||
T, | ||
K | ||
> extends true | ||
? True | ||
: False | ||
|
||
export type { IfRequiredKey as default } |
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,19 @@ | ||
/** | ||
* @file Type Definitions - IsRequiredKey | ||
* @module tutils/types/IsRequiredKey | ||
*/ | ||
|
||
import type IndexSignature from './index-signature' | ||
import type RequiredKeys from './keys-required' | ||
|
||
/** | ||
* Returns a boolean indicating if `K` is a required property of `T`. | ||
* | ||
* @template T - Type to evaluate | ||
* @template K - Key to evaluate | ||
*/ | ||
type IsRequiredKey<T, K extends IndexSignature> = K extends RequiredKeys<T> | ||
? true | ||
: false | ||
|
||
export type { IsRequiredKey as default } |
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