-
-
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):
IsOptionalKey
, IfOptionalKey
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
77e6053
commit c0ebf6f
Showing
10 changed files
with
140 additions
and
33 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,28 @@ | ||
/** | ||
* @file Type Tests - IfOptionalKey | ||
* @module tutils/types/tests/unit-d/IfOptionalKey | ||
*/ | ||
|
||
import type Author from '#fixtures/author.interface' | ||
import type TestSubject from '../if-key-optional' | ||
|
||
describe('unit-d:types/IfOptionalKey', () => { | ||
type False = false | ||
type True = true | ||
|
||
it('should equal False if IsOptionalKey<T, K> extends false', () => { | ||
// Arrange | ||
type K = 'last_name' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<Author, K, True, False>>().toEqualTypeOf<False>() | ||
}) | ||
|
||
it('should equal True if IsOptionalKey<T, K> extends true', () => { | ||
// Arrange | ||
type K = 'email' | ||
|
||
// 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,29 @@ | ||
/** | ||
* @file Type Tests - IsOptionalKey | ||
* @module tutils/types/tests/unit-d/IsOptionalKey | ||
*/ | ||
|
||
import type Author from '#fixtures/author.interface' | ||
import type TestSubject from '../is-key-optional' | ||
|
||
describe('unit-d:types/IsOptionalKey', () => { | ||
it('should equal false if K is not optional property of T', () => { | ||
// Arrange | ||
type K1 = 'first_name' | ||
type K2 = 'last_name' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<Author, K1>>().toEqualTypeOf<false>() | ||
expectTypeOf<TestSubject<Author, K2>>().toEqualTypeOf<false>() | ||
}) | ||
|
||
it('should equal true if K is optional property of T', () => { | ||
// Arrange | ||
type K1 = 'display_name' | ||
type K2 = 'email' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<Author, K1>>().toEqualTypeOf<true>() | ||
expectTypeOf<TestSubject<Author, K2>>().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
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,27 @@ | ||
/** | ||
* @file Type Definitions - IfOptionalKey | ||
* @module tutils/types/IfOptionalKey | ||
*/ | ||
|
||
import type IndexSignature from './index-signature' | ||
import type IsOptionalKey from './is-key-optional' | ||
|
||
/** | ||
* Conditional type that resolves depending on whether or not `K` is an optional | ||
* property of `T`. | ||
* | ||
* @see {@linkcode IsOptionalKey} | ||
* | ||
* @template T - Type to evaluate | ||
* @template K - Key to evaluate | ||
* @template True - Type if `K` is optional property | ||
* @template False - Type if `K` is not optional property | ||
*/ | ||
type IfOptionalKey<T, K extends IndexSignature, True, False> = IsOptionalKey< | ||
T, | ||
K | ||
> extends true | ||
? True | ||
: False | ||
|
||
export type { IfOptionalKey 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 - IsOptionalKey | ||
* @module tutils/types/IsOptionalKey | ||
*/ | ||
|
||
import type IndexSignature from './index-signature' | ||
import type OptionalKeys from './keys-optional' | ||
|
||
/** | ||
* Returns a boolean indicating if `K` is an optional property of `T`. | ||
* | ||
* @template T - Type to evaluate | ||
* @template K - Key to evaluate | ||
*/ | ||
type IsOptionalKey<T, K extends IndexSignature> = K extends OptionalKeys<T> | ||
? true | ||
: false | ||
|
||
export type { IsOptionalKey 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