type-fest 2.8.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -37,6 +37,7 @@ export {SetReturnType} from './source/set-return-type';
37
37
  export {Asyncify} from './source/asyncify';
38
38
  export {Simplify} from './source/simplify';
39
39
  export {Jsonify} from './source/jsonify';
40
+ export {LiteralToPrimitive} from './source/literal-to-primitive';
40
41
  export {
41
42
  PositiveInfinity,
42
43
  NegativeInfinity,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
package/readme.md CHANGED
@@ -88,10 +88,6 @@ Click the type names for complete docs.
88
88
  - [`Class`](source/basic.d.ts) - Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
89
89
  - [`Constructor`](source/basic.d.ts) - Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
90
90
  - [`TypedArray`](source/typed-array.d.ts) - Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
91
- - [`JsonPrimitive`](source/basic.d.ts) - Matches a JSON primitive.
92
- - [`JsonObject`](source/basic.d.ts) - Matches a JSON object.
93
- - [`JsonArray`](source/basic.d.ts) - Matches a JSON array.
94
- - [`JsonValue`](source/basic.d.ts) - Matches any valid JSON value.
95
91
  - [`ObservableLike`](source/observable-like.d.ts) - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
96
92
 
97
93
  ### Utilities
@@ -114,15 +110,23 @@ Click the type names for complete docs.
114
110
  - [`ConditionalPick`](source/conditional-pick.d.ts) - Like `Pick` except it selects properties from a shape where the values extend the given `Condition` type.
115
111
  - [`ConditionalExcept`](source/conditional-except.d.ts) - Like `Omit` except it removes properties from a shape where the values extend the given `Condition` type.
116
112
  - [`UnionToIntersection`](source/union-to-intersection.d.ts) - Convert a union type to an intersection type.
113
+ - [`LiteralToPrimitive`](source/literal-to-primitive.d.ts) - Convert a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types) to the [primitive type](source/primitive.d.ts) it belongs to.
117
114
  - [`Stringified`](source/stringified.d.ts) - Create a type with the keys of the given type changed to `string` type.
118
115
  - [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example, an array or a generator.
119
116
  - [`Entry`](source/entry.d.ts) - Create a type that represents the type of an entry of a collection.
120
117
  - [`Entries`](source/entries.d.ts) - Create a type that represents the type of the entries of a collection.
121
118
  - [`SetReturnType`](source/set-return-type.d.ts) - Create a function type with a return type of your choice and the same parameters as the given function type.
122
119
  - [`Simplify`](source/simplify.d.ts) - Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
123
- - [`Jsonify`](source/jsonify.d.ts) - Transform a type to one that is assignable to the `JsonValue` type.
124
120
  - [`Get`](source/get.d.ts) - Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs/latest#get) function.
125
121
 
122
+ ### JSON
123
+
124
+ - [`Jsonify`](source/jsonify.d.ts) - Transform a type to one that is assignable to the `JsonValue` type.
125
+ - [`JsonPrimitive`](source/basic.d.ts) - Matches a JSON primitive.
126
+ - [`JsonObject`](source/basic.d.ts) - Matches a JSON object.
127
+ - [`JsonArray`](source/basic.d.ts) - Matches a JSON array.
128
+ - [`JsonValue`](source/basic.d.ts) - Matches any valid JSON value.
129
+
126
130
  ### Async
127
131
 
128
132
  - [`Promisable`](source/promisable.d.ts) - Create a type that represents either the value or the value wrapped in `PromiseLike`.
package/source/get.d.ts CHANGED
@@ -1,16 +1,43 @@
1
1
  import {StringDigit} from '../source/utilities';
2
2
  import {Split} from './split';
3
3
 
4
+ type GetOptions = {
5
+ strict?: boolean;
6
+ };
7
+
4
8
  /**
5
9
  Like the `Get` type but receives an array of strings as a path parameter.
6
10
  */
7
- type GetWithPath<BaseType, Keys extends readonly string[]> =
11
+ type GetWithPath<BaseType, Keys extends readonly string[], Options extends GetOptions = {}> =
8
12
  Keys extends []
9
13
  ? BaseType
10
14
  : Keys extends [infer Head, ...infer Tail]
11
- ? GetWithPath<PropertyOf<BaseType, Extract<Head, string>>, Extract<Tail, string[]>>
15
+ ? GetWithPath<
16
+ PropertyOf<BaseType, Extract<Head, string>, Options>,
17
+ Extract<Tail, string[]>,
18
+ Options
19
+ >
12
20
  : never;
13
21
 
22
+ /**
23
+ Adds `undefined` to `Type` if `strict` is enabled.
24
+ */
25
+ type Strictify<Type, Options extends GetOptions> =
26
+ Options['strict'] extends true ? Type | undefined : Type;
27
+
28
+ /**
29
+ If `Options['strict']` is `true`, includes `undefined` in the returned type when accessing properties on `Record<string, any>`.
30
+
31
+ Known limitations:
32
+ - Does not include `undefined` in the type on object types with an index signature (for example, `{a: string; [key: string]: string}`).
33
+ */
34
+ type StrictPropertyOf<BaseType, Key extends keyof BaseType, Options extends GetOptions> =
35
+ Record<string, any> extends BaseType
36
+ ? string extends keyof BaseType
37
+ ? Strictify<BaseType[Key], Options> // Record<string, any>
38
+ : BaseType[Key] // Record<'a' | 'b', any> (Records with a string union as keys have required properties)
39
+ : BaseType[Key];
40
+
14
41
  /**
15
42
  Splits a dot-prop style path into a tuple comprised of the properties in the path. Handles square-bracket notation.
16
43
 
@@ -29,8 +56,12 @@ type ToPath<S extends string> = Split<FixPathSquareBrackets<S>, '.'>;
29
56
  Replaces square-bracketed dot notation with dots, for example, `foo[0].bar` -> `foo.0.bar`.
30
57
  */
31
58
  type FixPathSquareBrackets<Path extends string> =
32
- Path extends `${infer Head}[${infer Middle}]${infer Tail}`
33
- ? `${Head}.${Middle}${FixPathSquareBrackets<Tail>}`
59
+ Path extends `[${infer Head}]${infer Tail}`
60
+ ? Tail extends `[${string}`
61
+ ? `${Head}.${FixPathSquareBrackets<Tail>}`
62
+ : `${Head}${FixPathSquareBrackets<Tail>}`
63
+ : Path extends `${infer Head}[${infer Middle}]${infer Tail}`
64
+ ? `${Head}.${FixPathSquareBrackets<`[${Middle}]${Tail}`>}`
34
65
  : Path;
35
66
 
36
67
  /**
@@ -76,11 +107,11 @@ Note:
76
107
  - 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.
77
108
  - Returns `undefined` from nullish values, to match the behaviour of most deep-key libraries like `lodash`, `dot-prop`, etc.
78
109
  */
79
- type PropertyOf<BaseType, Key extends string> =
110
+ type PropertyOf<BaseType, Key extends string, Options extends GetOptions = {}> =
80
111
  BaseType extends null | undefined
81
112
  ? undefined
82
113
  : Key extends keyof BaseType
83
- ? BaseType[Key]
114
+ ? StrictPropertyOf<BaseType, Key, Options>
84
115
  : BaseType extends [] | [unknown, ...unknown[]]
85
116
  ? unknown // It's a tuple, but `Key` did not extend `keyof BaseType`. So the index is out of bounds.
86
117
  : BaseType extends {
@@ -89,11 +120,11 @@ type PropertyOf<BaseType, Key extends string> =
89
120
  }
90
121
  ? (
91
122
  ConsistsOnlyOf<Key, StringDigit> extends true
92
- ? Item
123
+ ? Strictify<Item, Options>
93
124
  : unknown
94
125
  )
95
126
  : Key extends keyof WithStringKeys<BaseType>
96
- ? WithStringKeys<BaseType>[Key]
127
+ ? StrictPropertyOf<WithStringKeys<BaseType>, Key, Options>
97
128
  : unknown;
98
129
 
99
130
  // 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.
@@ -128,10 +159,15 @@ interface ApiResponse {
128
159
  const getName = (apiResponse: ApiResponse) =>
129
160
  get(apiResponse, 'hits.hits[0]._source.name');
130
161
  //=> Array<{given: string[]; family: string}>
162
+
163
+ // Strict mode:
164
+ Get<string[], '3', {strict: true}> //=> string | undefined
165
+ Get<Record<string, string>, 'foo', {strict: true}> // => string | undefined
131
166
  ```
132
167
 
133
- @category Template literal
134
168
  @category Object
135
169
  @category Array
170
+ @category Template literal
136
171
  */
137
- export type Get<BaseType, Path extends string> = GetWithPath<BaseType, ToPath<Path>>;
172
+ export type Get<BaseType, Path extends string, Options extends GetOptions = {}> =
173
+ GetWithPath<BaseType, ToPath<Path>, Options>;
@@ -70,6 +70,6 @@ type Jsonify<T> =
70
70
  ? (() => J) extends (() => JsonValue) // Is J assignable to JsonValue?
71
71
  ? J // Then T is Jsonable and its Jsonable value is J
72
72
  : never // Not Jsonable because its toJSON() method does not return JsonValue
73
- : {[P in keyof T]: Jsonify<T[P]>} // It's an object: recursive call for its children
73
+ : {[P in keyof T]: Jsonify<Required<T>[P]>} // It's an object: recursive call for its children
74
74
  : never // Otherwise any other non-object is removed
75
75
  : never; // Otherwise non-JSONable type union was found not empty
@@ -0,0 +1,36 @@
1
+ /**
2
+ Given a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types) return the {@link Primitive | primitive type} it belongs to, or `never` if it's not a primitive.
3
+
4
+ Use-case: Working with generic types that may be literal types.
5
+
6
+ @example
7
+ ```
8
+ import {LiteralToPrimitive} from 'type-fest';
9
+
10
+ // No overloads needed to get the correct return type
11
+ function plus<T extends number | bigint | string>(x: T, y: T): LiteralToPrimitive<T> {
12
+ return x + (y as any);
13
+ }
14
+
15
+ plus('a', 'b'); // string
16
+ plus(1, 2); // number
17
+ plus(1n, 2n); // bigint
18
+ ```
19
+
20
+ @category Type
21
+ */
22
+ export type LiteralToPrimitive<T> = T extends number
23
+ ? number
24
+ : T extends bigint
25
+ ? bigint
26
+ : T extends string
27
+ ? string
28
+ : T extends boolean
29
+ ? boolean
30
+ : T extends symbol
31
+ ? symbol
32
+ : T extends null
33
+ ? null
34
+ : T extends undefined
35
+ ? undefined
36
+ : never;