type-fest 2.12.2 → 2.14.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.
Files changed (70) hide show
  1. package/index.d.ts +5 -1
  2. package/package.json +4 -4
  3. package/readme.md +74 -2
  4. package/source/async-return-type.d.ts +2 -2
  5. package/source/asyncify.d.ts +3 -3
  6. package/source/camel-case.d.ts +3 -3
  7. package/source/camel-cased-properties-deep.d.ts +3 -1
  8. package/source/camel-cased-properties.d.ts +3 -1
  9. package/source/conditional-except.d.ts +4 -4
  10. package/source/conditional-keys.d.ts +3 -1
  11. package/source/conditional-pick.d.ts +3 -3
  12. package/source/delimiter-case.d.ts +2 -2
  13. package/source/delimiter-cased-properties-deep.d.ts +3 -1
  14. package/source/delimiter-cased-properties.d.ts +3 -1
  15. package/source/entries.d.ts +2 -2
  16. package/source/entry.d.ts +1 -1
  17. package/source/exact.d.ts +51 -0
  18. package/source/except.d.ts +2 -2
  19. package/source/fixed-length-array.d.ts +4 -1
  20. package/source/get.d.ts +4 -4
  21. package/source/includes.d.ts +2 -2
  22. package/source/internal.d.ts +10 -1
  23. package/source/invariant-of.d.ts +5 -1
  24. package/source/iterable-element.d.ts +6 -0
  25. package/source/join.d.ts +1 -1
  26. package/source/jsonify.d.ts +5 -1
  27. package/source/kebab-case.d.ts +2 -2
  28. package/source/kebab-cased-properties-deep.d.ts +3 -1
  29. package/source/kebab-cased-properties.d.ts +3 -1
  30. package/source/last-array-element.d.ts +1 -1
  31. package/source/literal-to-primitive.d.ts +1 -1
  32. package/source/literal-union.d.ts +2 -2
  33. package/source/merge-exclusive.d.ts +1 -1
  34. package/source/merge.d.ts +3 -3
  35. package/source/multidimensional-array.d.ts +2 -2
  36. package/source/multidimensional-readonly-array.d.ts +2 -2
  37. package/source/mutable.d.ts +3 -38
  38. package/source/numeric.d.ts +5 -5
  39. package/source/opaque.d.ts +34 -1
  40. package/source/package-json.d.ts +8 -8
  41. package/source/partial-deep.d.ts +2 -2
  42. package/source/pascal-case.d.ts +2 -2
  43. package/source/pascal-cased-properties-deep.d.ts +3 -1
  44. package/source/pascal-cased-properties.d.ts +3 -1
  45. package/source/promisable.d.ts +1 -1
  46. package/source/promise-value.d.ts +4 -4
  47. package/source/readonly-deep.d.ts +2 -2
  48. package/source/readonly-tuple.d.ts +41 -0
  49. package/source/remove-index-signature.d.ts +8 -2
  50. package/source/replace.d.ts +67 -0
  51. package/source/require-all-or-none.d.ts +1 -1
  52. package/source/require-at-least-one.d.ts +2 -2
  53. package/source/require-exactly-one.d.ts +1 -1
  54. package/source/schema.d.ts +1 -1
  55. package/source/screaming-snake-case.d.ts +4 -4
  56. package/source/set-optional.d.ts +3 -3
  57. package/source/set-required.d.ts +3 -3
  58. package/source/set-return-type.d.ts +1 -1
  59. package/source/simplify.d.ts +2 -2
  60. package/source/snake-case.d.ts +2 -2
  61. package/source/snake-cased-properties-deep.d.ts +3 -1
  62. package/source/snake-cased-properties.d.ts +3 -1
  63. package/source/split.d.ts +1 -1
  64. package/source/string-key-of.d.ts +1 -1
  65. package/source/stringified.d.ts +1 -1
  66. package/source/trim.d.ts +1 -1
  67. package/source/union-to-intersection.d.ts +2 -2
  68. package/source/value-of.d.ts +1 -1
  69. package/source/writable.d.ts +40 -0
  70. package/license +0 -1
package/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export * from './source/observable-like';
7
7
  // Utilities
8
8
  export {Except} from './source/except';
9
9
  export {Mutable} from './source/mutable';
10
+ export {Writable} from './source/writable';
10
11
  export {Merge} from './source/merge';
11
12
  export {MergeExclusive} from './source/merge-exclusive';
12
13
  export {RequireAtLeastOne} from './source/require-at-least-one';
@@ -17,7 +18,7 @@ export {PartialDeep} from './source/partial-deep';
17
18
  export {ReadonlyDeep} from './source/readonly-deep';
18
19
  export {LiteralUnion} from './source/literal-union';
19
20
  export {Promisable} from './source/promisable';
20
- export {Opaque} from './source/opaque';
21
+ export {Opaque, UnwrapOpaque} from './source/opaque';
21
22
  export {InvariantOf} from './source/invariant-of';
22
23
  export {SetOptional} from './source/set-optional';
23
24
  export {SetRequired} from './source/set-required';
@@ -54,6 +55,8 @@ export {
54
55
  NonNegativeInteger,
55
56
  } from './source/numeric';
56
57
  export {StringKeyOf} from './source/string-key-of';
58
+ export {Exact} from './source/exact';
59
+ export {ReadonlyTuple} from './source/readonly-tuple';
57
60
 
58
61
  // Template literal types
59
62
  export {CamelCase} from './source/camel-case';
@@ -75,6 +78,7 @@ export {DelimiterCasedPropertiesDeep} from './source/delimiter-cased-properties-
75
78
  export {Join} from './source/join';
76
79
  export {Split} from './source/split';
77
80
  export {Trim} from './source/trim';
81
+ export {Replace} from './source/replace';
78
82
  export {Includes} from './source/includes';
79
83
  export {Get} from './source/get';
80
84
  export {LastArrayElement} from './source/last-array-element';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "2.12.2",
3
+ "version": "2.14.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -33,9 +33,9 @@
33
33
  ],
34
34
  "devDependencies": {
35
35
  "@sindresorhus/tsconfig": "~0.7.0",
36
- "expect-type": "^0.12.0",
37
- "tsd": "^0.17.0",
38
- "typescript": ">=4.2",
36
+ "expect-type": "^0.13.0",
37
+ "tsd": "^0.20.0",
38
+ "typescript": "^4.6.3",
39
39
  "xo": "^0.43.0"
40
40
  },
41
41
  "types": "./index.d.ts",
package/readme.md CHANGED
@@ -9,6 +9,7 @@
9
9
  <br>
10
10
  <br>
11
11
  <br>
12
+ <hr>
12
13
  <div align="center">
13
14
  <p>
14
15
  <p>
@@ -24,6 +25,7 @@
24
25
  </a>
25
26
  <br>
26
27
  <br>
28
+ <br>
27
29
  <a href="https://workos.com/?utm_campaign=github_repo&utm_medium=referral&utm_content=type-fest&utm_source=github">
28
30
  <div>
29
31
  <img src="https://sindresorhus.com/assets/thanks/workos-logo-white-bg.svg" width="220" alt="WorkOS">
@@ -35,6 +37,72 @@
35
37
  <sup>Add Single Sign-On (and more) in minutes instead of months.</sup>
36
38
  </div>
37
39
  </a>
40
+ <br>
41
+ <br>
42
+ <br>
43
+ <a href="https://neverinstall.com/spaces/devtools?utm_source=github&utm_medium=sponsor&utm_campaign=sindre#gh-light-mode-only">
44
+ <div>
45
+ <img src="https://sindresorhus.com/assets/thanks/neverinstall-logo-light.svg" width="200" alt="neverinstall">
46
+ </div>
47
+ <br>
48
+ <b>All your favourite IDE's now available on the cloud</b>
49
+ <div>
50
+ <sub>
51
+ Neverinstall gives you an uninterrupted development experience and improved accessibility,
52
+ <br>
53
+ allowing you to code faster, better and on-the-go on your favourite IDEs like
54
+ <br>
55
+ Android Studio, VS Code, Jupyter and PyCharm using your browser.
56
+ </sub>
57
+ </div>
58
+ </a>
59
+ <a href="https://neverinstall.com/spaces/devtools?utm_source=github&utm_medium=sponsor&utm_campaign=sindre#gh-dark-mode-only">
60
+ <div>
61
+ <img src="https://sindresorhus.com/assets/thanks/neverinstall-logo-dark.svg" width="200" alt="neverinstall">
62
+ </div>
63
+ <br>
64
+ <b>All your favourite IDE's now available on the cloud</b>
65
+ <div>
66
+ <sub>
67
+ Neverinstall gives you an uninterrupted development experience and improved accessibility,
68
+ <br>
69
+ allowing you to code faster, better and on-the-go on your favourite IDEs like
70
+ <br>
71
+ Android Studio, VS Code, Jupyter and PyCharm using your browser.
72
+ </sub>
73
+ </div>
74
+ </a>
75
+ <br>
76
+ <br>
77
+ <br>
78
+ <a href="https://www.useanvil.com/?utm_source=sindresorhus#gh-light-mode-only">
79
+ <div>
80
+ <img src="https://sindresorhus.com/assets/thanks/anvil-logo-light.svg" width="200" alt="Anvil">
81
+ </div>
82
+ <br>
83
+ <b>Paperwork that makes the data work.</b>
84
+ <div>
85
+ <sub>
86
+ Easy APIs for paperwork. PDF generation, e-signature and embeddable no-code webforms.
87
+ <br>
88
+ The easiest way to build paperwork automation into your product.
89
+ </sub>
90
+ </div>
91
+ </a>
92
+ <a href="https://www.useanvil.com/?utm_source=sindresorhus#gh-dark-mode-only">
93
+ <div>
94
+ <img src="https://sindresorhus.com/assets/thanks/anvil-logo-dark.svg" width="200" alt="Anvil">
95
+ </div>
96
+ <br>
97
+ <b>Paperwork that makes the data work.</b>
98
+ <div>
99
+ <sub>
100
+ Easy APIs for paperwork. PDF generation, e-signature and embeddable no-code webforms.
101
+ <br>
102
+ The easiest way to build paperwork automation into your product.
103
+ </sub>
104
+ </div>
105
+ </a>
38
106
  </p>
39
107
  </div>
40
108
  <br>
@@ -67,7 +135,7 @@ npm install type-fest
67
135
  ## Usage
68
136
 
69
137
  ```ts
70
- import {Except} from 'type-fest';
138
+ import type {Except} from 'type-fest';
71
139
 
72
140
  type Foo = {
73
141
  unicorn: string;
@@ -93,7 +161,7 @@ Click the type names for complete docs.
93
161
  ### Utilities
94
162
 
95
163
  - [`Except`](source/except.d.ts) - Create a type from an object type without certain keys. This is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys).
96
- - [`Mutable`](source/mutable.d.ts) - Create a type that strips `readonly` from all or some of an object's keys. The inverse of `Readonly<T>`.
164
+ - [`Writable`](source/writable.d.ts) - Create a type that strips `readonly` from all or some of an object's keys. The inverse of `Readonly<T>`. Formerly named `Mutable`.
97
165
  - [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type.
98
166
  - [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive keys.
99
167
  - [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given keys.
@@ -104,6 +172,7 @@ Click the type names for complete docs.
104
172
  - [`ReadonlyDeep`](source/readonly-deep.d.ts) - Create a deeply immutable version of an `object`/`Map`/`Set`/`Array` type. Use [`Readonly<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype) if you only need one level deep.
105
173
  - [`LiteralUnion`](source/literal-union.d.ts) - Create a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729).
106
174
  - [`Opaque`](source/opaque.d.ts) - Create an [opaque type](https://codemix.com/opaque-types-in-javascript/).
175
+ - [`UnwrapOpaque`](source/opaque.d.ts) - Revert an [opaque type](https://codemix.com/opaque-types-in-javascript/) back to its original type.
107
176
  - [`InvariantOf`](source/invariant-of.d.ts) - Create an [invariant type](https://basarat.gitbook.io/typescript/type-system/type-compatibility#footnote-invariance), which is a type that does not accept supertypes and subtypes.
108
177
  - [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
109
178
  - [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required.
@@ -122,6 +191,7 @@ Click the type names for complete docs.
122
191
  - [`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.
123
192
  - [`StringKeyOf`](source/string-key-of.d.ts) - Get keys of the given type as strings.
124
193
  - [`Schema`](source/schema.d.ts) - Create a deep version of another object type where property values are recursively replaced into a given value type.
194
+ - [`Exact`](source/exact.d.ts) - Create a type that does not allow extra properties.
125
195
 
126
196
  ### JSON
127
197
 
@@ -141,6 +211,7 @@ Click the type names for complete docs.
141
211
 
142
212
  - [`Trim`](source/trim.d.ts) - Remove leading and trailing spaces from a string.
143
213
  - [`Split`](source/split.d.ts) - Represents an array of strings split using a given character or character set.
214
+ - [`Replace`](source/replace.d.ts) - Represents a string with some or all matches replaced by a replacement.
144
215
 
145
216
  ### Array
146
217
 
@@ -150,6 +221,7 @@ Click the type names for complete docs.
150
221
  - [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length.
151
222
  - [`MultidimensionalArray`](source/multidimensional-array.d.ts) - Create a type that represents a multidimensional array of the given type and dimensions.
152
223
  - [`MultidimensionalReadonlyArray`](source/multidimensional-readonly-array.d.ts) - Create a type that represents a multidimensional readonly array of the given type and dimensions.
224
+ - [`ReadonlyTuple`](source/readonly-tuple.d.ts) - Create a type that represents a read-only tuple of the given type and length.
153
225
 
154
226
  ### Numeric
155
227
 
@@ -1,4 +1,4 @@
1
- import {PromiseValue} from './promise-value';
1
+ import type {PromiseValue} from './promise-value';
2
2
 
3
3
  type AsyncFunction = (...args: any[]) => Promise<unknown>;
4
4
 
@@ -9,7 +9,7 @@ There has been [discussion](https://github.com/microsoft/TypeScript/pull/35998)
9
9
 
10
10
  @example
11
11
  ```ts
12
- import {AsyncReturnType} from 'type-fest';
12
+ import type {AsyncReturnType} from 'type-fest';
13
13
  import {asyncFunction} from 'api';
14
14
 
15
15
  // This type resolves to the unwrapped return type of `asyncFunction`.
@@ -1,5 +1,5 @@
1
- import {PromiseValue} from './promise-value';
2
- import {SetReturnType} from './set-return-type';
1
+ import type {PromiseValue} from './promise-value';
2
+ import type {SetReturnType} from './set-return-type';
3
3
 
4
4
  /**
5
5
  Create an async version of the given function type, by boxing the return type in `Promise` while keeping the same parameter types.
@@ -8,7 +8,7 @@ Use-case: You have two functions, one synchronous and one asynchronous that do t
8
8
 
9
9
  @example
10
10
  ```
11
- import {Asyncify} from 'type-fest';
11
+ import type {Asyncify} from 'type-fest';
12
12
 
13
13
  // Synchronous function.
14
14
  function getFooSync(someArg: SomeType): Foo {
@@ -1,5 +1,5 @@
1
- import {WordSeparators} from '../source/utilities';
2
- import {Split} from './split';
1
+ import type {WordSeparators} from '../source/utilities';
2
+ import type {Split} from './split';
3
3
 
4
4
  /**
5
5
  Step by step takes the first item in an array literal, formats it and adds it to a string literal, and then recursively appends the remainder.
@@ -36,7 +36,7 @@ This can be useful when, for example, converting some kebab-cased command-line f
36
36
 
37
37
  @example
38
38
  ```
39
- import {CamelCase} from 'type-fest';
39
+ import type {CamelCase} from 'type-fest';
40
40
 
41
41
  // Simple
42
42
 
@@ -1,4 +1,4 @@
1
- import {CamelCase} from './camel-case';
1
+ import type {CamelCase} from './camel-case';
2
2
 
3
3
  /**
4
4
  Convert object properties to camel case recursively.
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
10
10
 
11
11
  @example
12
12
  ```
13
+ import type {CamelCasedPropertiesDeep} from 'type-fest';
14
+
13
15
  interface User {
14
16
  UserId: number;
15
17
  UserName: string;
@@ -1,4 +1,4 @@
1
- import {CamelCase} from './camel-case';
1
+ import type {CamelCase} from './camel-case';
2
2
 
3
3
  /**
4
4
  Convert object properties to camel case but not recursively.
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
10
10
 
11
11
  @example
12
12
  ```
13
+ import type {CamelCasedProperties} from 'type-fest';
14
+
13
15
  interface User {
14
16
  UserId: number;
15
17
  UserName: string;
@@ -1,5 +1,5 @@
1
- import {Except} from './except';
2
- import {ConditionalKeys} from './conditional-keys';
1
+ import type {Except} from './except';
2
+ import type {ConditionalKeys} from './conditional-keys';
3
3
 
4
4
  /**
5
5
  Exclude keys from a shape that matches the given `Condition`.
@@ -8,7 +8,7 @@ This is useful when you want to create a new type with a specific set of keys fr
8
8
 
9
9
  @example
10
10
  ```
11
- import {Primitive, ConditionalExcept} from 'type-fest';
11
+ import type {Primitive, ConditionalExcept} from 'type-fest';
12
12
 
13
13
  class Awesome {
14
14
  name: string;
@@ -24,7 +24,7 @@ type ExceptPrimitivesFromAwesome = ConditionalExcept<Awesome, Primitive>;
24
24
 
25
25
  @example
26
26
  ```
27
- import {ConditionalExcept} from 'type-fest';
27
+ import type {ConditionalExcept} from 'type-fest';
28
28
 
29
29
  interface Example {
30
30
  a: string;
@@ -5,7 +5,7 @@ Internally this is used for the `ConditionalPick` and `ConditionalExcept` types.
5
5
 
6
6
  @example
7
7
  ```
8
- import {ConditionalKeys} from 'type-fest';
8
+ import type {ConditionalKeys} from 'type-fest';
9
9
 
10
10
  interface Example {
11
11
  a: string;
@@ -22,6 +22,8 @@ To support partial types, make sure your `Condition` is a union of undefined (fo
22
22
 
23
23
  @example
24
24
  ```
25
+ import type {ConditionalKeys} from 'type-fest';
26
+
25
27
  type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>;
26
28
  //=> 'a' | 'c'
27
29
  ```
@@ -1,4 +1,4 @@
1
- import {ConditionalKeys} from './conditional-keys';
1
+ import type {ConditionalKeys} from './conditional-keys';
2
2
 
3
3
  /**
4
4
  Pick keys from the shape that matches the given `Condition`.
@@ -7,7 +7,7 @@ This is useful when you want to create a new type from a specific subset of an e
7
7
 
8
8
  @example
9
9
  ```
10
- import {Primitive, ConditionalPick} from 'type-fest';
10
+ import type {Primitive, ConditionalPick} from 'type-fest';
11
11
 
12
12
  class Awesome {
13
13
  name: string;
@@ -23,7 +23,7 @@ type PickPrimitivesFromAwesome = ConditionalPick<Awesome, Primitive>;
23
23
 
24
24
  @example
25
25
  ```
26
- import {ConditionalPick} from 'type-fest';
26
+ import type {ConditionalPick} from 'type-fest';
27
27
 
28
28
  interface Example {
29
29
  a: string;
@@ -1,4 +1,4 @@
1
- import {UpperCaseCharacters, WordSeparators} from '../source/utilities';
1
+ import type {UpperCaseCharacters, WordSeparators} from '../source/utilities';
2
2
 
3
3
  /**
4
4
  Unlike a simpler split, this one includes the delimiter splitted on in the resulting array literal. This is to enable splitting on, for example, upper-case characters.
@@ -54,7 +54,7 @@ This can be useful when, for example, converting a camel-cased object property t
54
54
 
55
55
  @example
56
56
  ```
57
- import {DelimiterCase} from 'type-fest';
57
+ import type {DelimiterCase} from 'type-fest';
58
58
 
59
59
  // Simple
60
60
 
@@ -1,4 +1,4 @@
1
- import {DelimiterCase} from './delimiter-case';
1
+ import type {DelimiterCase} from './delimiter-case';
2
2
 
3
3
  /**
4
4
  Convert object properties to delimiter case recursively.
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
10
10
 
11
11
  @example
12
12
  ```
13
+ import type {DelimiterCasedPropertiesDeep} from 'type-fest';
14
+
13
15
  interface User {
14
16
  userId: number;
15
17
  userName: string;
@@ -1,4 +1,4 @@
1
- import {DelimiterCase} from './delimiter-case';
1
+ import type {DelimiterCase} from './delimiter-case';
2
2
 
3
3
  /**
4
4
  Convert object properties to delimiter case but not recursively.
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
10
10
 
11
11
  @example
12
12
  ```
13
+ import type {DelimiterCasedProperties} from 'type-fest';
14
+
13
15
  interface User {
14
16
  userId: number;
15
17
  userName: string;
@@ -1,4 +1,4 @@
1
- import {ArrayEntry, MapEntry, ObjectEntry, SetEntry} from './entry';
1
+ import type {ArrayEntry, MapEntry, ObjectEntry, SetEntry} from './entry';
2
2
 
3
3
  type ArrayEntries<BaseType extends readonly unknown[]> = Array<ArrayEntry<BaseType>>;
4
4
  type MapEntries<BaseType> = Array<MapEntry<BaseType>>;
@@ -14,7 +14,7 @@ For example the {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/R
14
14
 
15
15
  @example
16
16
  ```
17
- import {Entries} from 'type-fest';
17
+ import type {Entries} from 'type-fest';
18
18
 
19
19
  interface Example {
20
20
  someKey: number;
package/source/entry.d.ts CHANGED
@@ -15,7 +15,7 @@ For example the {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/R
15
15
 
16
16
  @example
17
17
  ```
18
- import {Entry} from 'type-fest';
18
+ import type {Entry} from 'type-fest';
19
19
 
20
20
  interface Example {
21
21
  someKey: number;
@@ -0,0 +1,51 @@
1
+ import type {Primitive} from './primitive';
2
+ import type {KeysOfUnion} from './internal';
3
+
4
+ /**
5
+ Create a type that does not allow extra properties, meaning it only allows properties that are explicitly declared.
6
+
7
+ This is useful for function type-guarding to reject arguments with excess properties. Due to the nature of TypeScript, it does not complain if excess properties are provided unless the provided value is an object literal.
8
+
9
+ *Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/12936) if you want to have this type as a built-in in TypeScript.*
10
+
11
+ @example
12
+ ```
13
+ type OnlyAcceptName = {name: string};
14
+
15
+ function onlyAcceptName(args: OnlyAcceptName) {}
16
+
17
+ // TypeScript complains about excess properties when an object literal is provided.
18
+ onlyAcceptName({name: 'name', id: 1});
19
+ //=> `id` is excess
20
+
21
+ // TypeScript does not complain about excess properties when the provided value is a variable (not an object literal).
22
+ const invalidInput = {name: 'name', id: 1};
23
+ onlyAcceptName(invalidInput); // No errors
24
+ ```
25
+
26
+ Having `Exact` allows TypeScript to reject excess properties.
27
+
28
+ @example
29
+ ```
30
+ import {Exact} from 'type-fest';
31
+
32
+ type OnlyAcceptName = {name: string};
33
+
34
+ function onlyAcceptNameImproved<T extends Exact<OnlyAcceptName, T>>(args: T) {}
35
+
36
+ const invalidInput = {name: 'name', id: 1};
37
+ onlyAcceptNameImproved(invalidInput); // Compilation error
38
+ ```
39
+
40
+ [Read more](https://stackoverflow.com/questions/49580725/is-it-possible-to-restrict-typescript-object-to-contain-only-properties-defined)
41
+
42
+ @category Utilities
43
+ */
44
+ export type Exact<ParameterType, InputType extends ParameterType> = ParameterType extends Primitive
45
+ ? ParameterType
46
+ /*
47
+ Create a type from `ParameterType` and `InputType` and change keys exclusive to `InputType` to `never`.
48
+ - Generate a list of keys that exists in `InputType` but not in `ParameterType`.
49
+ - Mark these excess keys as `never`.
50
+ */
51
+ : {[Key in keyof ParameterType]: Exact<ParameterType[Key], InputType[Key]>} & Record<Exclude<keyof InputType, KeysOfUnion<ParameterType>>, never>;
@@ -1,4 +1,4 @@
1
- import {IsEqual} from './internal';
1
+ import type {IsEqual} from './internal';
2
2
 
3
3
  /**
4
4
  Filter out keys from an object.
@@ -38,7 +38,7 @@ This type was proposed to the TypeScript team, which declined it, saying they pr
38
38
 
39
39
  @example
40
40
  ```
41
- import {Except} from 'type-fest';
41
+ import type {Except} from 'type-fest';
42
42
 
43
43
  type Foo = {
44
44
  a: number;
@@ -13,9 +13,11 @@ Use-cases:
13
13
  - Creating a range union (for example, `0 | 1 | 2 | 3 | 4` from the keys of such a type) without having to resort to recursive types.
14
14
  - Creating an array of coordinates with a static length, for example, length of 3 for a 3D vector.
15
15
 
16
+ Note: This type does not prevent out-of-bounds access. Prefer `ReadonlyTuple` unless you need mutability.
17
+
16
18
  @example
17
19
  ```
18
- import {FixedLengthArray} from 'type-fest';
20
+ import type {FixedLengthArray} from 'type-fest';
19
21
 
20
22
  type FencingTeam = FixedLengthArray<string, 3>;
21
23
 
@@ -29,6 +31,7 @@ guestFencingTeam.push('Sam');
29
31
  ```
30
32
 
31
33
  @category Array
34
+ @see ReadonlyTuple
32
35
  */
33
36
  export type FixedLengthArray<Element, Length extends number, ArrayPrototype = [Element, ...Element[]]> = Pick<
34
37
  ArrayPrototype,
package/source/get.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import {StringDigit} from '../source/utilities';
2
- import {Split} from './split';
3
- import {StringKeyOf} from './string-key-of';
1
+ import type {StringDigit} from '../source/utilities';
2
+ import type {Split} from './split';
3
+ import type {StringKeyOf} from './string-key-of';
4
4
 
5
5
  type GetOptions = {
6
6
  strict?: boolean;
@@ -141,7 +141,7 @@ Use-case: Retrieve a property from deep inside an API response or some other com
141
141
 
142
142
  @example
143
143
  ```
144
- import {Get} from 'type-fest';
144
+ import type {Get} from 'type-fest';
145
145
  import * as lodash from 'lodash';
146
146
 
147
147
  const get = <BaseType, Path extends string | readonly string[]>(object: BaseType, path: Path): Get<BaseType, Path> =>
@@ -1,4 +1,4 @@
1
- import {IsEqual} from './internal';
1
+ import type {IsEqual} from './internal';
2
2
 
3
3
  /**
4
4
  Returns a boolean for whether the given array includes the given item.
@@ -7,7 +7,7 @@ This can be useful if another type wants to make a decision based on whether the
7
7
 
8
8
  @example
9
9
  ```
10
- import {Includes} from 'type-fest';
10
+ import type {Includes} from 'type-fest';
11
11
 
12
12
  type hasRed<array extends any[]> = Includes<array, 'red'>;
13
13
  ```
@@ -1,4 +1,4 @@
1
- import {Primitive} from './primitive';
1
+ import type {Primitive} from './primitive';
2
2
 
3
3
  /**
4
4
  Returns a boolean for whether the two given types are equal.
@@ -42,3 +42,12 @@ export type Subtract<A extends number, B extends number> = BuildTuple<A> extends
42
42
  Matches any primitive, `Date`, or `RegExp` value.
43
43
  */
44
44
  export type BuiltIns = Primitive | Date | RegExp;
45
+
46
+ /**
47
+ Gets keys from a type. Similar to `keyof` but this one also works for union types.
48
+
49
+ The reason a simple `keyof Union` does not work is because `keyof` always returns the accessible keys of a type. In the case of a union, that will only be the common keys.
50
+
51
+ @link https://stackoverflow.com/a/49402091
52
+ */
53
+ export type KeysOfUnion<T> = T extends T ? keyof T : never;
@@ -1,4 +1,4 @@
1
- import {Opaque} from './opaque';
1
+ import type {Opaque} from './opaque';
2
2
 
3
3
  /**
4
4
  Create an [invariant type](https://basarat.gitbook.io/typescript/type-system/type-compatibility#footnote-invariance), which is a type that does not accept supertypes and subtypes.
@@ -9,6 +9,8 @@ Use-case:
9
9
 
10
10
  @example
11
11
  ```
12
+ import type {InvariantOf} from 'type-fest';
13
+
12
14
  class Animal {
13
15
  constructor(public name: string){}
14
16
  }
@@ -32,6 +34,8 @@ invariantAnimalArray = invariantCatArray; // Error: Type 'InvariantOf<Cat>[]' is
32
34
 
33
35
  @example
34
36
  ```
37
+ import type {InvariantOf} from 'type-fest';
38
+
35
39
  // In covariance (default)
36
40
 
37
41
  interface FooBar {
@@ -9,6 +9,8 @@ Here is an example of `IterableElement` in action with a generator function:
9
9
 
10
10
  @example
11
11
  ```
12
+ import type {IterableElement} from 'type-fest';
13
+
12
14
  function * iAmGenerator() {
13
15
  yield 1;
14
16
  yield 2;
@@ -21,6 +23,8 @@ And here is an example with an async generator:
21
23
 
22
24
  @example
23
25
  ```
26
+ import type {IterableElement} from 'type-fest';
27
+
24
28
  async function * iAmGeneratorAsync() {
25
29
  yield 'hi';
26
30
  yield true;
@@ -35,6 +39,8 @@ An example with an array of strings:
35
39
 
36
40
  @example
37
41
  ```
42
+ import type {IterableElement} from 'type-fest';
43
+
38
44
  type MeString = IterableElement<string[]>
39
45
  ```
40
46
 
package/source/join.d.ts CHANGED
@@ -5,7 +5,7 @@ Use-case: Defining key paths in a nested object. For example, for dot-notation f
5
5
 
6
6
  @example
7
7
  ```
8
- import {Join} from 'type-fest';
8
+ import type {Join} from 'type-fest';
9
9
 
10
10
  // Mixed (strings & numbers) items; result is: 'foo.0.baz'
11
11
  const path: Join<['foo', 0, 'baz'], '.'> = ['foo', 0, 'baz'].join('.');
@@ -1,4 +1,4 @@
1
- import {JsonPrimitive, JsonValue} from './basic';
1
+ import type {JsonPrimitive, JsonValue} from './basic';
2
2
 
3
3
  // Note: The return value has to be `any` and not `unknown` so it can match `void`.
4
4
  type NotJsonable = ((...args: any[]) => any) | undefined;
@@ -23,6 +23,8 @@ An interface cannot be structurally compared to `JsonValue` because an interface
23
23
 
24
24
  @example
25
25
  ```
26
+ import type {Jsonify} from 'type-fest';
27
+
26
28
  interface Geometry {
27
29
  type: 'Point' | 'Polygon';
28
30
  coordinates: [number, number];
@@ -51,6 +53,8 @@ Non-JSON values such as `Date` implement `.toJSON()`, so they can be transformed
51
53
 
52
54
  @example
53
55
  ```
56
+ import type {Jsonify} from 'type-fest';
57
+
54
58
  const time = {
55
59
  timeValue: new Date()
56
60
  };
@@ -1,4 +1,4 @@
1
- import {DelimiterCase} from './delimiter-case';
1
+ import type {DelimiterCase} from './delimiter-case';
2
2
 
3
3
  /**
4
4
  Convert a string literal to kebab-case.
@@ -7,7 +7,7 @@ This can be useful when, for example, converting a camel-cased object property t
7
7
 
8
8
  @example
9
9
  ```
10
- import {KebabCase} from 'type-fest';
10
+ import type {KebabCase} from 'type-fest';
11
11
 
12
12
  // Simple
13
13