-
-
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.
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
- Loading branch information
1 parent
41f9463
commit 42ef8cd
Showing
10 changed files
with
181 additions
and
14 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
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,17 @@ | ||
/** | ||
* @file Type Tests - PickNative | ||
* @module tutils/types/tests/unit-d/PickNative | ||
*/ | ||
|
||
import type Author from '#fixtures/author.interface' | ||
import type TestSubject from '../pick-native' | ||
|
||
describe('unit-d:types/PickNative', () => { | ||
it('should equal typescript.Pick<T, K>', () => { | ||
// Arrange | ||
type K = 'email' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<Author, K>>().toEqualTypeOf<Pick<Author, K>> | ||
}) | ||
}) |
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 @@ | ||
/** | ||
* @file Type Tests - Pick | ||
* @module tutils/types/tests/unit-d/Pick | ||
*/ | ||
|
||
import type Author from '#fixtures/author.interface' | ||
import type Book from '#fixtures/book.interface' | ||
import type Publisher from '#fixtures/publisher.interface' | ||
import type TestSubject from '../pick' | ||
import type PickNative from '../pick-native' | ||
|
||
describe('unit-d:types/Pick', () => { | ||
it('should equal PickNative<T, K> if K extends keyof T', () => { | ||
// Arrange | ||
type T = Book | ||
type K = 'isbn' | 'title' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<T, K>>().toEqualTypeOf<PickNative<T, K>>() | ||
}) | ||
|
||
it('should pick properties with respect for dot notation', () => { | ||
// Arrange | ||
type K = | ||
| 'authors.0.display_name' | ||
| 'authors.0.email' | ||
| 'isbn' | ||
| 'publisher.name' | ||
| 'title' | ||
|
||
// Expect | ||
expectTypeOf<TestSubject<Book, K>>().toEqualTypeOf<{ | ||
authors: { | ||
display_name?: Author['display_name'] | ||
email?: Exclude<Author['email'], undefined> | ||
}[] | ||
publisher?: PickNative<Publisher, 'name'> | ||
isbn: Book['isbn'] | ||
title: Book['title'] | ||
}>() | ||
}) | ||
}) |
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,16 @@ | ||
/** | ||
* @file Type Definitions - PickNative | ||
* @module tutils/types/PickNative | ||
*/ | ||
|
||
/** | ||
* From `T`, pick a set of properties whose keys are in the union `K`. | ||
* | ||
* @see https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys | ||
* | ||
* @template T - Type to evaluate | ||
* @template K - Keys to select | ||
*/ | ||
type PickNative<T, K extends keyof T> = Pick<T, K> | ||
|
||
export type { PickNative 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @file Type Definitions - Pick | ||
* @module tutils/types/Pick | ||
*/ | ||
|
||
import type At from './at' | ||
import type EnsureString from './ensure-string' | ||
import type Get from './get' | ||
import type Head from './head' | ||
import type IfOptionalKey from './if-key-optional' | ||
import type IfExactOptionalKey from './if-key-optional-exact' | ||
import type IfRequiredKey from './if-key-required' | ||
import type IfNever from './if-never' | ||
import type IfTuple from './if-tuple' | ||
import type IfUndefined from './if-undefined' | ||
import type Join from './join' | ||
import type NIL from './nil' | ||
import type NumberString from './number-string' | ||
import type Numeric from './numeric' | ||
import type Simplify from './simplify' | ||
import type Tail from './tail' | ||
|
||
/** | ||
* From `T`, pick a set of properties whose keys are in the union `K`. | ||
* | ||
* Supports dot-notation for nested paths and array indexing. | ||
* | ||
* @template T - Type to evaluate | ||
* @template K - Keys to select | ||
*/ | ||
type Pick<T, K extends NumberString> = Simplify< | ||
{ | ||
[H in Head<`${K}`>]: Get<T, H> extends infer U | ||
? Join<[H, NumberString]> extends infer J | ||
? Extract<K, J> extends infer X | ||
? IfNever< | ||
X, | ||
U, | ||
Tail<X> extends infer P | ||
? NonNullable<U> extends readonly unknown[] | ||
? Head<P> extends infer HNext | ||
? HNext extends Numeric | number | ||
? At<NonNullable<U>, HNext> extends infer Item | ||
? IfUndefined< | ||
Item, | ||
[Item], | ||
Pick< | ||
NonNullable<Item>, | ||
EnsureString<Tail<P>> | ||
> extends infer Next | ||
? IfTuple<NonNullable<U>, [Next], Next[]> | ||
: never | ||
> | ||
: never | ||
: Pick<NonNullable<U>, EnsureString<P>> | ||
: never | ||
: Pick<NonNullable<U>, EnsureString<P>> | ||
: never | ||
> | ||
: never | ||
: never | ||
: never | ||
} extends infer R | ||
? NonNullable<T> extends readonly unknown[] | ||
? Extract<T, NIL> | R | ||
: { | ||
[K in keyof R as IfExactOptionalKey<T, K, K, never>]?: Exclude< | ||
R[K], | ||
undefined | ||
> | ||
} & { | ||
[K in keyof R as IfOptionalKey< | ||
T, | ||
K, | ||
IfExactOptionalKey<T, K, never, K>, | ||
never | ||
>]?: R[K] | ||
} & { | ||
[K in keyof R as IfRequiredKey<T, K, K, never>]: R[K] | ||
} | ||
: never | ||
> | ||
|
||
export type { Pick as default } |