forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.d.ts
131 lines (113 loc) · 4.08 KB
/
get.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import {Split} from './utilities';
import {StringDigit} from '../source/utilities';
/**
Like the `Get` type but receives an array of strings as a path parameter.
*/
type GetWithPath<BaseType, Keys extends readonly string[]> =
Keys extends []
? BaseType
: Keys extends [infer Head, ...infer Tail]
? GetWithPath<PropertyOf<BaseType, Extract<Head, string>>, Extract<Tail, string[]>>
: never;
/**
Splits a dot-prop style path into a tuple comprised of the properties in the path. Handles square-bracket notation.
@example
```
ToPath<'foo.bar.baz'>
//=> ['foo', 'bar', 'baz']
ToPath<'foo[0].bar.baz'>
//=> ['foo', '0', 'bar', 'baz']
```
*/
type ToPath<S extends string> = Split<FixPathSquareBrackets<S>, '.'>;
/**
Replaces square-bracketed dot notation with dots, for example, `foo[0].bar` -> `foo.0.bar`.
*/
type FixPathSquareBrackets<Path extends string> =
Path extends `${infer Head}[${infer Middle}]${infer Tail}`
? `${Head}.${Middle}${FixPathSquareBrackets<Tail>}`
: Path;
/**
Returns true if `LongString` is made up out of `Substring` repeated 0 or more times.
@example
```
ConsistsOnlyOf<'aaa', 'a'> //=> true
ConsistsOnlyOf<'ababab', 'ab'> //=> true
ConsistsOnlyOf<'aBa', 'a'> //=> false
ConsistsOnlyOf<'', 'a'> //=> true
```
*/
type ConsistsOnlyOf<LongString extends string, Substring extends string> =
LongString extends ''
? true
: LongString extends `${Substring}${infer Tail}`
? ConsistsOnlyOf<Tail, Substring>
: false;
/**
Convert a type which may have number keys to one with string keys, making it possible to index using strings retrieved from template types.
@example
```
type WithNumbers = {foo: string; 0: boolean};
type WithStrings = WithStringKeys<WithNumbers>;
type WithNumbersKeys = keyof WithNumbers;
//=> 'foo' | 0
type WithStringsKeys = keyof WithStrings;
//=> 'foo' | '0'
```
*/
type WithStringKeys<BaseType extends Record<string | number, any>> = {
[Key in `${Extract<keyof BaseType, string | number>}`]: BaseType[Key]
};
/**
Get a property of an object or array. Works when indexing arrays using number-literal-strings, for example, `PropertyOf<number[], '0'> = number`, and when indexing objects with number keys.
Note:
- Returns `unknown` if `Key` is not a property of `BaseType`, since TypeScript uses structural typing, and it cannot be guaranteed that extra properties unknown to the type system will exist at runtime.
- Returns `undefined` from nullish values, to match the behaviour of most deep-key libraries like `lodash`, `dot-prop`, etc.
*/
type PropertyOf<BaseType, Key extends string> =
BaseType extends null | undefined
? undefined
: Key extends keyof BaseType
? BaseType[Key]
: BaseType extends {
[n: number]: infer Item;
length: number; // Note: This is needed to avoid being too lax with records types using number keys like `{0: string; 1: boolean}`.
}
? (
ConsistsOnlyOf<Key, StringDigit> extends true
? Item
: unknown
)
: Key extends keyof WithStringKeys<BaseType>
? WithStringKeys<BaseType>[Key]
: unknown;
// This works by first splitting the path based on `.` and `[...]` characters into a tuple of string keys. Then it recursively uses the head key to get the next property of the current object, until there are no keys left. Number keys extract the item type from arrays, or are converted to strings to extract types from tuples and dictionaries with number keys.
/**
Get a deeply-nested property from an object using a key path, like Lodash's `.get()` function.
Use-case: Retrieve a property from deep inside an API response or some other complex object.
@example
```
import {Get} from 'type-fest';
import * as lodash from 'lodash';
const get = <BaseType, Path extends string>(object: BaseType, path: Path): Get<BaseType, Path> =>
lodash.get(object, path);
interface ApiResponse {
hits: {
hits: Array<{
_id: string
_source: {
name: Array<{
given: string[]
family: string
}>
birthDate: string
}
}>
}
}
const getName = (apiResponse: ApiResponse) =>
get(apiResponse, 'hits.hits[0]._source.name');
//=> Array<{given: string[]; family: string}>
```
*/
export type Get<BaseType, Path extends string> = GetWithPath<BaseType, ToPath<Path>>;