Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / ansi-escapes / node_modules / type-fest / readme.md
1 <div align="center">
2         <br>
3         <br>
4         <img src="media/logo.svg" alt="type-fest" height="300">
5         <br>
6         <br>
7         <b>A collection of essential TypeScript types</b>
8         <br>
9         <hr>
10 </div>
11 <br>
12 <br>
13
14 [![Build Status](https://travis-ci.com/sindresorhus/type-fest.svg?branch=master)](https://travis-ci.com/sindresorhus/type-fest)
15 [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4)
16 <!-- Commented out until they actually show anything
17 [![npm dependents](https://badgen.net/npm/dependents/type-fest)](https://www.npmjs.com/package/type-fest?activeTab=dependents) [![npm downloads](https://badgen.net/npm/dt/type-fest)](https://www.npmjs.com/package/type-fest)
18 -->
19
20 Many of the types here should have been built-in. You can help by suggesting some of them to the [TypeScript project](https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md).
21
22 Either add this package as a dependency or copy-paste the needed types. No credit required. ðŸ‘Œ
23
24 PR welcome for additional commonly needed types and docs improvements. Read the [contributing guidelines](.github/contributing.md) first.
25
26 ## Install
27
28 ```
29 $ npm install type-fest
30 ```
31
32 *Requires TypeScript >=3.2*
33
34 ## Usage
35
36 ```ts
37 import {Except} from 'type-fest';
38
39 type Foo = {
40         unicorn: string;
41         rainbow: boolean;
42 };
43
44 type FooWithoutRainbow = Except<Foo, 'rainbow'>;
45 //=> {unicorn: string}
46 ```
47
48 ## API
49
50 Click the type names for complete docs.
51
52 ### Basic
53
54 - [`Primitive`](source/basic.d.ts) - Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
55 - [`Class`](source/basic.d.ts) - Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
56 - [`TypedArray`](source/basic.d.ts) - Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
57 - [`JsonObject`](source/basic.d.ts) - Matches a JSON object.
58 - [`JsonArray`](source/basic.d.ts) - Matches a JSON array.
59 - [`JsonValue`](source/basic.d.ts) - Matches any valid JSON value.
60 - [`ObservableLike`](source/basic.d.ts) - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
61
62 ### Utilities
63
64 - [`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/release-notes/typescript-3-5.html#the-omit-helper-type).
65 - [`Mutable`](source/mutable.d.ts) - Convert an object with `readonly` keys into a mutable object. The inverse of `Readonly<T>`.
66 - [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type.
67 - [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive keys.
68 - [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given keys.
69 - [`RequireExactlyOne`](source/require-exactly-one.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more.
70 - [`PartialDeep`](source/partial-deep.d.ts) - Create a deeply optional version of another type. Use [`Partial<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1401-L1406) if you only need one level deep.
71 - [`ReadonlyDeep`](source/readonly-deep.d.ts) - Create a deeply immutable version of an `object`/`Map`/`Set`/`Array` type. Use [`Readonly<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1415-L1420) if you only need one level deep.
72 - [`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).
73 - [`Promisable`](source/promisable.d.ts) - Create a type that represents either the value or the value wrapped in `PromiseLike`.
74 - [`Opaque`](source/opaque.d.ts) - Create an [opaque type](https://codemix.com/opaque-types-in-javascript/).
75 - [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
76 - [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required.
77 - [`PromiseValue`](source/promise-value.d.ts) - Returns the type that is wrapped inside a `Promise`.
78 - [`AsyncReturnType`](source/async-return-type.d.ts) - Unwrap the return type of a function that returns a `Promise`.
79 - [`ConditionalKeys`](source/conditional-keys.d.ts) - Extract keys from a shape where values extend the given `Condition` type.
80 - [`ConditionalPick`](source/conditional-pick.d.ts) - Like `Pick` except it selects properties from a shape where the values extend the given `Condition` type.
81 - [`ConditionalExcept`](source/conditional-except.d.ts) - Like `Omit` except it removes properties from a shape where the values extend the given `Condition` type.
82
83 ### Miscellaneous
84
85 - [`PackageJson`](source/package-json.d.ts) - Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file).
86 - [`TsConfigJson`](source/tsconfig-json.d.ts) - Type for [TypeScript's `tsconfig.json` file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) (TypeScript 3.7).
87
88 ## Declined types
89
90 *If we decline a type addition, we will make sure to document the better solution here.*
91
92 - [`Diff` and `Spread`](https://github.com/sindresorhus/type-fest/pull/7) - The PR author didn't provide any real-world use-cases and the PR went stale. If you think this type is useful, provide some real-world use-cases and we might reconsider.
93 - [`Dictionary`](https://github.com/sindresorhus/type-fest/issues/33) - You only save a few characters (`Dictionary<number>` vs `Record<string, number>`) from [`Record`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1429-L1434), which is more flexible and well-known. Also, you shouldn't use an object as a dictionary. We have `Map` in JavaScript now.
94
95 ## Tips
96
97 ### Built-in types
98
99 There are many advanced types most users don't know about.
100
101 - [`Partial<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1401-L1406) - Make all properties in `T` optional.
102         <details>
103         <summary>
104                         Example
105         </summary>
106
107         [Playground](https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgHIHsAmEDC6QzADmyA3gLABQyycADnanALYQBcyAzmFKEQNxUaddFDAcQAV2YAjaIMoBfKlQQAbOJ05osEAIIMAQpOBrsUMkOR1eANziRkCfISKSoD4Pg4ZseAsTIALyW1DS0DEysHADkvvoMMQA0VsKi4sgAzAAMuVaKClY2wPaOknSYDrguADwA0sgQAB6QIJjaANYQAJ7oMDp+LsQAfAAUXd0cdUnI9mo+uv6uANp1ALoAlKHhyGAAFsCcAHTOAW4eYF4gyxNrwbNwago0ypRWp66jH8QcAApwYmAjxq8SWIy2FDCNDA3ToKFBQyIdR69wmfQG1TOhShyBgomQX3w3GQE2Q6IA8jIAFYQBBgI4TTiEs5bTQYsFInrLTbbHZOIlgZDlSqQABqj0kKBC3yINx6a2xfOQwH6o2FVXFaklwSCIUkbQghBAEEwENSfNOlykEGefNe5uhB2O6sgS3GPRmLogmslG1tLxUOKgEDA7hAuydtteryAA)
108
109         ```ts
110         interface NodeConfig {
111                         appName: string;
112                         port: number;
113         }
114
115         class NodeAppBuilder {
116                         private configuration: NodeConfig = {
117                                         appName: 'NodeApp',
118                                         port: 3000
119                         };
120
121                         private updateConfig<Key extends keyof NodeConfig>(key: Key, value: NodeConfig[Key]) {
122                                         this.configuration[key] = value;
123                         }
124
125                         config(config: Partial<NodeConfig>) {
126                                         type NodeConfigKey = keyof NodeConfig;
127
128                                         for (const key of Object.keys(config) as NodeConfigKey[]) {
129                                                         const updateValue = config[key];
130
131                                                         if (updateValue === undefined) {
132                                                                         continue;
133                                                         }
134
135                                                         this.updateConfig(key, updateValue);
136                                         }
137
138                                         return this;
139                         }
140         }
141
142         // `Partial<NodeConfig>`` allows us to provide only a part of the
143         // NodeConfig interface.
144         new NodeAppBuilder().config({appName: 'ToDoApp'});
145         ```
146         </details>
147
148 - [`Required<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1408-L1413) - Make all properties in `T` required.
149         <details>
150         <summary>
151                         Example
152         </summary>
153
154         [Playground](https://typescript-play.js.org/?target=6#code/AQ4SwOwFwUwJwGYEMDGNgGED21VQGJZwC2wA3gFCjXAzFJgA2A-AFzADOUckA5gNxUaIYjA4ckvGG07c+g6gF8KQkAgCuEFFDA5O6gEbEwUbLm2ESwABQIixACJIoSdgCUYAR3Vg4MACYAPGYuFvYAfACU5Ko0APRxwADKMBD+wFAAFuh2Vv7OSBlYGdmc8ABu8LHKsRyGxqY4oQT21pTCIHQMjOwA5DAAHgACxAAOjDAAdChYxL0ANLHUouKSMH0AEmAAhJhY6ozpAJ77GTCMjMCiV0ToSAb7UJPPC9WRgrEJwAAqR6MwSRQPFGUFocDgRHYxnEfGAowh-zgUCOwF6KwkUl6tXqJhCeEsxDaS1AXSYfUGI3GUxmc0WSneQA)
155
156         ```ts
157         interface ContactForm {
158                         email?: string;
159                         message?: string;
160         }
161
162         function submitContactForm(formData: Required<ContactForm>) {
163                         // Send the form data to the server.
164         }
165
166         submitContactForm({
167                         email: 'ex@mple.com',
168                         message: 'Hi! Could you tell me more about…',
169         });
170
171         // TypeScript error: missing property 'message'
172         submitContactForm({
173                         email: 'ex@mple.com',
174         });
175         ```
176         </details>
177
178 - [`Readonly<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1415-L1420) - Make all properties in `T` readonly.
179         <details>
180         <summary>
181                         Example
182         </summary>
183
184         [Playground](https://typescript-play.js.org/?target=6#code/AQ4UwOwVwW2AZA9gc3mAbmANsA3gKFCOAHkAzMgGkOJABEwAjKZa2kAUQCcvEu32AMQCGAF2FYBIAL4BufDRABLCKLBcywgMZgEKZOoDCiCGSXI8i4hGEwwALmABnUVxXJ57YFgzZHSVF8sT1BpBSItLGEnJz1kAy5LLy0TM2RHACUwYQATEywATwAeAITjU3MAPnkrCJMXLigtUT4AClxgGztKbyDgaX99I1TzAEokr1BRAAslJwA6FIqLAF48TtswHp9MHDla9hJGACswZvmyLjAwAC8wVpm5xZHkUZDaMKIwqyWXYCW0oN4sNlsA1h0ug5gAByACyBQAggAHJHQ7ZBIFoXbzBjMCz7OoQP5YIaJNYQMAAdziCVaALGNSIAHomcAACoFJFgADKWjcSNEwG4vC4ji0wggEEQguiTnMEGALWAV1yAFp8gVgEjeFyuKICvMrCTgVxnst5jtsGC4ljsPNhXxGaAWcAAOq6YRXYDCRg+RWIcA5JSC+kWdCepQ+v3RYCU3RInzRMCGwlpC19NYBW1Ye08R1AA)
185
186         ```ts
187         enum LogLevel {
188                         Off,
189                         Debug,
190                         Error,
191                         Fatal
192         };
193
194         interface LoggerConfig {
195                         name: string;
196                         level: LogLevel;
197         }
198
199         class Logger {
200                         config: Readonly<LoggerConfig>;
201
202                         constructor({name, level}: LoggerConfig) {
203                                         this.config = {name, level};
204                                         Object.freeze(this.config);
205                         }
206         }
207
208         const config: LoggerConfig = {
209                 name: 'MyApp',
210                 level: LogLevel.Debug
211         };
212
213         const logger = new Logger(config);
214
215         // TypeScript Error: cannot assign to read-only property.
216         logger.config.level = LogLevel.Error;
217
218         // We are able to edit config variable as we please.
219         config.level = LogLevel.Error;
220         ```
221         </details>
222
223 - [`Pick<T, K>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1422-L1427) - From `T`, pick a set of properties whose keys are in the union `K`.
224         <details>
225         <summary>
226                         Example
227         </summary>
228
229         [Playground](https://typescript-play.js.org/?target=6#code/AQ4SwOwFwUwJwGYEMDGNgEE5TCgNugN4BQoZwOUBAXMAM5RyQDmA3KeSFABYCuAtgCMISMHloMmENh04oA9tBjQJjFuzIBfYrOAB6PcADCcGElh1gEGAHcKATwAO6ebyjB5CTNlwFwSxFR0BX5HeToYABNgBDh5fm8cfBg6AHIKG3ldA2BHOOcfFNpUygJ0pAhokr4hETFUgDpswywkggAFUwA3MFtgAF5gQgowKhhVKTYKGuFRcXo1aVZgbTIoJ3RW3xhOmB6+wfbcAGsAHi3kgBpgEtGy4AAfG54BWfqAPnZm4AAlZUj4MAkMA8GAGB4vEgfMlLLw6CwPBA8PYRmMgZVgAC6CgmI4cIommQELwICh8RBgKZKvALh1ur0bHQABR5PYMui0Wk7em2ADaAF0AJS0AASABUALIAGQAogR+Mp3CROCAFBBwVC2ikBpj5CgBIqGjizLA5TAFdAmalImAuqlBRoVQh5HBgEy1eDWfs7J5cjzGYKhroVfpDEhHM4MV6GRR5NN0JrtnRg6BVirTFBeHAKYmYY6QNpdB73LmCJZBlSAXAubtvczeSmQMNSuMbmKNgBlHFgPEUNwusBIPAAQlS1xetTmxT0SDoESgdD0C4aACtHMwxytLrohawgA)
230
231         ```ts
232         interface Article {
233                         title: string;
234                         thumbnail: string;
235                         content: string;
236         }
237
238         // Creates new type out of the `Article` interface composed
239         // from the Articles' two properties: `title` and `thumbnail`.
240         // `ArticlePreview = {title: string; thumbnail: string}`
241         type ArticlePreview = Pick<Article, 'title' | 'thumbnail'>;
242
243         // Render a list of articles using only title and description.
244         function renderArticlePreviews(previews: ArticlePreview[]): HTMLElement {
245                         const articles = document.createElement('div');
246
247                         for (const preview of previews) {
248                                         // Append preview to the articles.
249                         }
250
251                         return articles;
252         }
253
254         const articles = renderArticlePreviews([
255                         {
256                                 title: 'TypeScript tutorial!',
257                                 thumbnail: '/assets/ts.jpg'
258                         }
259         ]);
260         ```
261         </details>
262
263 - [`Record<K, T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1429-L1434) - Construct a type with a set of properties `K` of type `T`.
264         <details>
265         <summary>
266                         Example
267         </summary>
268
269         [Playground](https://typescript-play.js.org/?target=6#code/AQ4ejYAUHsGcCWAXBMB2dgwGbAKYC2ADgDYwCeeemCaWArgE7ADGMxAhmuQHQBQoYEnJE8wALKEARnkaxEKdMAC8wAOS0kstGuAAfdQBM8ANzxlRjXQbVaWACwC0JPB0NqA3HwGgIwAJJoWozYHCxixnAsjAhStADmwESMMJYo1Fi4HMCIaPEu+MRklHj8gpqyoeHAAKJFFFTAAN4+giDYCIxwSAByHAR4AFw5SDF5Xm2gJBzdfQPD3WPxE5PAlBxdAPLYNQAelgh4aOHDaPQEMowrIAC+3oJ+AMKMrlrAXFhSAFZ4LEhC9g4-0BmA4JBISXgiCkBQABpILrJ5MhUGhYcATGD6Bk4Hh-jNgABrPDkOBlXyQAAq9ngYmJpOAAHcEOCRjAXqwYODfoo6DhakUSph+Uh7GI4P0xER4Cj0OSQGwMP8tP1hgAlX7swwAHgRl2RvIANALSA08ABtAC6AD4VM1Wm0Kow0MMrYaHYJjGYLLJXZb3at1HYnC43Go-QHQDcvA6-JsmEJXARgCDgMYWAhjIYhDAU+YiMAAFIwex0ZmilMITCGF79TLAGRsAgJYAAZRwSEZGzEABFTOZUrJ5Yn+jwnWgeER6HB7AAKJrADpdXqS4ZqYultTG6azVfqHswPBbtauLY7fayQ7HIbAAAMwBuAEoYw9IBq2Ixs9h2eFMOQYPQObALQKJgggABeYhghCIpikkKRpOQRIknAsZUiIeCttECBEP8NSMCkjDDAARMGziuIYxHwYOjDCMBmDNnAuTxA6irdCOBB1Lh5Dqpqn66tISIykawBnOCtqqC0gbjqc9DgpGkxegOliyfJDrRkAA)
270
271         ```ts
272         // Positions of employees in our company.
273         type MemberPosition = 'intern' | 'developer' | 'tech-lead';
274
275         // Interface describing properties of a single employee.
276         interface Employee {
277                         firstName: string;
278                         lastName: string;
279                         yearsOfExperience: number;
280         }
281
282         // Create an object that has all possible `MemberPosition` values set as keys.
283         // Those keys will store a collection of Employees of the same position.
284         const team: Record<MemberPosition, Employee[]> = {
285                         intern: [],
286                         developer: [],
287                         'tech-lead': [],
288         };
289
290         // Our team has decided to help John with his dream of becoming Software Developer.
291         team.intern.push({
292                 firstName: 'John',
293                 lastName: 'Doe',
294                 yearsOfExperience: 0
295         });
296
297         // `Record` forces you to initialize all of the property keys.
298         // TypeScript Error: "tech-lead" property is missing
299         const teamEmpty: Record<MemberPosition, null> = {
300                         intern: null,
301                         developer: null,
302         };
303         ```
304         </details>
305
306 - [`Exclude<T, U>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1436-L1439) - Exclude from `T` those types that are assignable to `U`.
307         <details>
308         <summary>
309                         Example
310         </summary>
311
312         [Playground](https://typescript-play.js.org/?target=6#code/JYOwLgpgTgZghgYwgAgMrQG7QMIHsQzADmyA3gFDLIAOuUYAXMiAK4A2byAPsgM5hRQJHqwC2AI2gBucgF9y5MAE9qKAEoQAjiwj8AEnBAATNtGQBeZAAooWphu26wAGmS3e93bRC8IASgsAPmRDJRlyAHoI5ABRAA8ENhYjFFYOZGVVZBgoXFFkAAM0zh5+QRBhZhYJaAKAOkjogEkQZAQ4X2QAdwALCFbaemRgXmQtFjhOMFwq9K6ULuB0lk6U+HYwZAxJnQaYFhAEMGB8ZCIIMAAFOjAANR2IK0HGWISklIAedCgsKDwCYgAbQA5M9gQBdVzFQJ+JhiSRQMiUYYwayZCC4VHPCzmSzAspCYEBWxgFhQAZwKC+FpgJ43VwARgADH4ZFQSWSBjcZPJyPtDsdTvxKWBvr8rD1DCZoJ5HPopaYoK4EPhCEQmGKcKriLCtrhgEYkVQVT5Nr4fmZLLZtMBbFZgT0wGBqES6ghbHBIJqoBKFdBWQpjfh+DQbhY2tqiHVsbjLMVkAB+ZAAZiZaeQTHOVxu9ySjxNaujNwDVHNvzqbBGkBAdPoAfkQA)
313
314         ```ts
315         interface ServerConfig {
316                 port: null | string | number;
317         }
318
319         type RequestHandler = (request: Request, response: Response) => void;
320
321         // Exclude `null` type from `null | string | number`.
322         // In case the port is equal to `null`, we will use default value.
323         function getPortValue(port: Exclude<ServerConfig['port'], null>): number {
324                 if (typeof port === 'string') {
325                         return parseInt(port, 10);
326                 }
327
328                 return port;
329         }
330
331         function startServer(handler: RequestHandler, config: ServerConfig): void {
332                 const server = require('http').createServer(handler);
333
334                 const port = config.port === null ? 3000 : getPortValue(config.port);
335                 server.listen(port);
336         }
337         ```
338         </details>
339
340 - [`Extract<T, U>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1441-L1444) - Extract from `T` those types that are assignable to `U`.
341         <details>
342         <summary>
343                         Example
344         </summary>
345
346         [Playground](https://typescript-play.js.org/?target=6#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXzSwEdkQBJYACgEoAueVZAWwCMQYBuAKDDwGcM8MgBF4AXngBlAJ6scESgHIRi6ty5ZUGdoihgEABXZ888AN5d48ANoiAuvUat23K6ihMQ9ATE0BzV3goPy8GZjZOLgBfLi4Aejj4AEEICBwAdz54MAALKFQQ+BxEeAAHY1NgKAwoIKy0grr4DByEUpgccpgMaXgAaxBerCzi+B9-ZulygDouFHRsU1z8kKMYE1RhaqgAHkt4AHkWACt4EAAPbVRgLLWNgBp9gGlBs8uQa6yAUUuYPQwdgNpKM7nh7mMML4CgA+R5WABqUAgpDeVxuhxO1he0jsXGh8EoOBO9COx3BQPo2PBADckaR6IjkSA6PBqTgsMBzPsicdrEC7OJWXSQNwYvFEgAVTS9JLXODpeDpKBZFg4GCoWa8VACIJykAKiQWKy2YQOAioYikCg0OEMDyhRSy4DyxS24KhAAMjyi6gS8AAwjh5OD0iBFHAkJoEOksC1mnkMJq8gUQKDNttKPlnfrwYp3J5XfBHXqoKpfYkAOI4ansTxaeDADmoRSCCBYAbxhC6TDx6rwYHIRX5bScjA4bLJwoDmDwDkfbA9JMrVMVdM1TN69LgkTgwgkchUahqIA)
347
348         ```ts
349         declare function uniqueId(): number;
350
351         const ID = Symbol('ID');
352
353         interface Person {
354                 [ID]: number;
355                 name: string;
356                 age: number;
357         }
358
359         // Allows changing the person data as long as the property key is of string type.
360         function changePersonData<
361                 Obj extends Person,
362                 Key extends Extract<keyof Person, string>,
363                 Value extends Obj[Key]
364         > (obj: Obj, key: Key, value: Value): void {
365                 obj[key] = value;
366         }
367
368         // Tiny Andrew was born.
369         const andrew = {
370                 [ID]: uniqueId(),
371                 name: 'Andrew',
372                 age: 0,
373         };
374
375         // Cool, we're fine with that.
376         changePersonData(andrew, 'name', 'Pony');
377
378         // Goverment didn't like the fact that you wanted to change your identity.
379         changePersonData(andrew, ID, uniqueId());
380         ```
381         </details>
382
383 - [`NonNullable<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1446-L1449) - Exclude `null` and `undefined` from `T`.
384         <details>
385         <summary>
386                         Example
387         </summary>
388         Works with <code>strictNullChecks</code> set to <code>true</code>. (Read more <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html">here</a>)
389
390         [Playground](https://typescript-play.js.org/?target=6#code/C4TwDgpgBACg9gJ2AOQK4FsBGEFQLxQDOwCAlgHYDmUAPlORtrnQwDasDcAUFwPQBU-WAEMkUOADMowqAGNWwwoSgATCBIqlgpOOSjAAFsOBRSy1IQgr9cKJlSlW1mZYQA3HFH68u8xcoBlHA8EACEHJ08Aby4oKDBUTFZSWXjEFEYcAEIALihkXTR2YSSIAB54JDQsHAA+blj4xOTUsHSACkMzPKD3HHDHNQQAGjSkPMqMmoQASh7g-oihqBi4uNIpdraxPAI2VhmVxrX9AzMAOm2ppnwoAA4ABifuE4BfKAhWSyOTuK7CS7pao3AhXF5rV48E4ICDAVAIPT-cGQyG+XTEIgLMJLTx7CAAdygvRCA0iCHaMwarhJOIQjUBSHaACJHk8mYdeLwxtdcVAAOSsh58+lXdr7Dlcq7A3n3J4PEUdADMcspUE53OluAIUGVTx46oAKuAIAFZGQwCYAKIIBCILjUxaDHAMnla+iodjcIA)
391
392         ```ts
393         type PortNumber = string | number | null;
394
395         /** Part of a class definition that is used to build a server */
396         class ServerBuilder {
397                         portNumber!: NonNullable<PortNumber>;
398
399                         port(this: ServerBuilder, port: PortNumber): ServerBuilder {
400                                         if (port == null) {
401                                                         this.portNumber = 8000;
402                                         } else {
403                                                         this.portNumber = port;
404                                         }
405
406                                         return this;
407                         }
408         }
409
410         const serverBuilder = new ServerBuilder();
411
412         serverBuilder
413                         .port('8000')   // portNumber = '8000'
414                         .port(null)     // portNumber =  8000
415                         .port(3000);    // portNumber =  3000
416
417         // TypeScript error
418         serverBuilder.portNumber = null;
419         ```
420         </details>
421
422 - [`Parameters<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1451-L1454) - Obtain the parameters of a function type in a tuple.
423         <details>
424         <summary>
425                         Example
426         </summary>
427
428         [Playground](https://typescript-play.js.org/?target=6#code/GYVwdgxgLglg9mABAZwBYmMANgUwBQxgAOIUAXIgIZgCeA2gLoCUFAbnDACaIDeAUIkQB6IYgCypSlBxUATrMo1ECsJzgBbLEoipqAc0J7EMKMgDkiHLnU4wp46pwAPHMgB0fAL58+oSLARECEosLAA5ABUYG2QAHgAxJGdpVWREPDdMylk9ZApqemZEAF4APipacrw-CApEgBogkKwAYThwckQwEHUAIxxZJl4BYVEImiIZKF0oZRwiWVdbeygJmThgOYgcGFYcbhqApCJsyhtpWXcR1cnEePBoeDAABVPzgbTixFeFd8uEsClADcIxGiygIFkSEOT3SmTc2VydQeRx+ZxwF2QQ34gkEwDgsnSuFmMBKiAADEDjIhYk1Qm0OlSYABqZnYka4xA1DJZHJYkGc7yCbyeRA+CAIZCzNAYbA4CIAdxg2zJwVCkWirjwMswuEaACYmCCgA)
429
430         ```ts
431         function shuffle(input: any[]): void {
432                 // Mutate array randomly changing its' elements indexes.
433         }
434
435         function callNTimes<Fn extends (...args: any[]) => any> (func: Fn, callCount: number) {
436                 // Type that represents the type of the received function parameters.
437                 type FunctionParameters = Parameters<Fn>;
438
439                 return function (...args: FunctionParameters) {
440                         for (let i = 0; i < callCount; i++) {
441                                 func(...args);
442                         }
443                 }
444         }
445
446         const shuffleTwice = callNTimes(shuffle, 2);
447         ```
448         </details>
449
450 - [`ConstructorParameters<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1456-L1459) - Obtain the parameters of a constructor function type in a tuple.
451         <details>
452         <summary>
453                         Example
454         </summary>
455
456         [Playground](https://typescript-play.js.org/?target=6#code/MYGwhgzhAECCBOAXAlqApgWQPYBM0mgG8AoaaFRENALmgkXmQDsBzAblOmCycTV4D8teo1YdO3JiICuwRFngAKClWENmLAJRFOZRAAtkEAHQq00ALzlklNBzIBfYk+KhIMAJJTEYJsDQAwmDA+mgAPAAq0GgAHnxMODCKTGgA7tCKxllg8CwQtL4AngDaALraFgB80EWa1SRkAA6MAG5gfNAB4FABPDJyCrQR9tDNyG0dwMGhtBhgjWEiGgA00F70vv4RhY3hEZXVVinpc42KmuJkkv3y8Bly8EPaDWTkhiZd7r3e8LK3llwGCMXGQWGhEOsfH5zJlsrl8p0+gw-goAAo5MAAW3BaHgEEilU0tEhmzQ212BJ0ry4SOg+kg+gBBiMximIGA0nAfAQLGk2N4EAAEgzYcYcnkLsRdDTvNEYkYUKwSdCme9WdM0MYwYhFPSIPpJdTkAAzDKxBUaZX+aAAQgsVmkCTQxuYaBw2ng4Ok8CYcotSu8pMur09iG9vuObxZnx6SN+AyUWTF8MN0CcZE4Ywm5jZHK5aB5fP4iCFIqT4oRRTKRLo6lYVNeAHpG50wOzOe1zHr9NLQ+HoABybsD4HOKXXRA1JCoKhBELmI5pNaB6Fz0KKBAodDYPAgSUTmqYsAALx4m5nC6nW9nGq14KtaEUA9gR9PvuNCjQ9BgACNvcwNBtAcLiAA)
457
458         ```ts
459         class ArticleModel {
460                 title: string;
461                 content?: string;
462
463                 constructor(title: string) {
464                         this.title = title;
465                 }
466         }
467
468         class InstanceCache<T extends (new (...args: any[]) => any)> {
469                 private ClassConstructor: T;
470                 private cache: Map<string, InstanceType<T>> = new Map();
471
472                 constructor (ctr: T) {
473                         this.ClassConstructor = ctr;
474                 }
475
476                 getInstance (...args: ConstructorParameters<T>): InstanceType<T> {
477                         const hash = this.calculateArgumentsHash(...args);
478
479                         const existingInstance = this.cache.get(hash);
480                         if (existingInstance !== undefined) {
481                                 return existingInstance;
482                         }
483
484                         return new this.ClassConstructor(...args);
485                 }
486
487                 private calculateArgumentsHash(...args: any[]): string {
488                         // Calculate hash.
489                         return 'hash';
490                 }
491         }
492
493         const articleCache = new InstanceCache(ArticleModel);
494         const amazonArticle = articleCache.getInstance('Amazon forests burining!');
495         ```
496         </details>
497
498 - [`ReturnType<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1461-L1464) â€“ Obtain the return type of a function type.
499         <details>
500         <summary>
501                         Example
502         </summary>
503
504         [Playground](https://typescript-play.js.org/?target=6#code/MYGwhgzhAECSAmICmBlJAnAbgS2E6A3gFDTTwD2AcuQC4AW2AdgOYAUAlAFzSbnbyEAvkWFFQkGJSQB3GMVI1sNZNwg10TZgG4S0YOUY0kh1es07d+xmvQBXYDXLpWi5UlMaWAGj0GjJ6BtNdkJdBQYIADpXZGgAXmgYpB1ScOwoq38aeN9DYxoU6GFRKzVoJjUwRjwAYXJbPPRuAFkwAAcAHgAxBodsAx9GWwBbACMMAD4cxhloVraOCyYjdAAzMDxoOut1e0d0UNIZ6WhWSPOwdGYIbiqATwBtAF0uaHudUQB6ACpv6ABpJBINqJdAbADW0Do5BOw3u5R2VTwMHIq2gAANtjZ0bkbHsnFCwJh8ONjHp0EgwEZ4JFoN9PkRVr1FAZoMwkDRYIjqkgOrosepoEgAB7+eAwAV2BxOLy6ACCVxgIrFEoMeOl6AACpcwMMORgIB1JRMiBNWKVdhruJKfOdIpdrtwFddXlzKjyACp3Nq842HaDIbL6BrZBIVGhIpB1EMYSLsmjmtWW-YhAA+qegAAYLKQLQj3ZsEsdccmnGcLor2Dn8xGedHGpEIBzEzspfsfMHDNAANTQACMVaIljV5GQkRA5DYmIpVKQAgAJARO9le33BDXIyi0YuLW2nJFGLqkOvxFB0YPdBSaLZ0IwNzyPkO8-xkGgsLh8Al427a3hWAhXwwHA8EHT5PmgAB1bAQBAANJ24adKWpft72RaBUTgRBUCAj89HAM8xCTaBjggABRQx0DuHJv25P9dCkWRZVIAAiBjoFImpmjlFBgA0NpsjadByDacgIDAEAIAAQmYpjoGYgAZSBsmGPw6DtZiiFA8CoJguDmAQmoZ2QvtUKQLdoAYmBTwgdEiCAA)
505
506         ```ts
507         /** Provides every element of the iterable `iter` into the `callback` function and stores the results in an array. */
508         function mapIter<
509                         Elem,
510                         Func extends (elem: Elem) => any,
511                         Ret extends ReturnType<Func>
512         >(iter: Iterable<Elem>, callback: Func): Ret[] {
513                         const mapped: Ret[] = [];
514
515                         for (const elem of iter) {
516                                         mapped.push(callback(elem));
517                         }
518
519                         return mapped;
520         }
521
522         const setObject: Set<string> = new Set();
523         const mapObject: Map<number, string> = new Map();
524
525         mapIter(setObject, (value: string) => value.indexOf('Foo')); // number[]
526
527         mapIter(mapObject, ([key, value]: [number, string]) => {
528                         return key % 2 === 0 ? value : 'Odd';
529         }); // string[]
530         ```
531         </details>
532
533 - [`InstanceType<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1466-L1469) â€“ Obtain the instance type of a constructor function type.
534         <details>
535         <summary>
536                         Example
537         </summary>
538
539         [Playground](https://typescript-play.js.org/?target=6#code/MYGwhgzhAECSAmICmBlJAnAbgS2E6A3gFDTTwD2AcuQC4AW2AdgOYAUAlAFzSbnbyEAvkWFFQkGJSQB3GMVI1sNZNwg10TZgG4S0YOUY0kh1es07d+xmvQBXYDXLpWi5UlMaWAGj0GjJ6BtNdkJdBQYIADpXZGgAXmgYpB1ScOwoq38aeN9DYxoU6GFRKzVoJjUwRjwAYXJbPPRuAFkwAAcAHgAxBodsAx9GWwBbACMMAD4cxhloVraOCyYjdAAzMDxoOut1e0d0UNIZ6WhWSPOwdGYIbiqATwBtAF0uaHudUQB6ACpv6ABpJBINqJdAbADW0Do5BOw3u5R2VTwMHIq2gAANtjZ0bkbHsnFCwJh8ONjHp0EgwEZ4JFoN9PkRVr1FAZoMwkDRYIjqkgOrosepoEgAB7+eAwAV2BxOLy6ACCVxgIrFEoMeOl6AACpcwMMORgIB1JRMiBNWKVdhruJKfOdIpdrtwFddXlzKjyACp3Nq842HaDIbL6BrZBIVGhIpB1EMYSLsmjmtWW-YhAA+qegAAYLKQLQj3ZsEsdccmnGcLor2Dn8xGedHGpEIBzEzspfsfMHDNAANTQACMVaIljV5GQkRA5DYmIpVKQAgAJARO9le33BDXIyi0YuLW2nJFGLqkOvxFB0YPdBSaLZ0IwNzyPkO8-xkGgsLh8Al427a3hWAhXwwHA8EHT5PmgAB1bAQBAANJ24adKWpft72RaBUTgRBUCAj89HAM8xCTaBjggABRQx0DuHJv25P9dCkWRZVIAAiBjoFImpmjlFBgA0NpsjadByDacgIDAEAIAAQmYpjoGYgAZSBsmGPw6DtZiiFA8CoJguDmAQmoZ2QvtUKQLdoAYmBTwgdEiCAA)
540
541         ```ts
542         class IdleService {
543                         doNothing (): void {}
544         }
545
546         class News {
547                         title: string;
548                         content: string;
549
550                         constructor(title: string, content: string) {
551                                         this.title = title;
552                                         this.content = content;
553                         }
554         }
555
556         const instanceCounter: Map<Function, number> = new Map();
557
558         interface Constructor {
559                         new(...args: any[]): any;
560         }
561
562         // Keep track how many instances of `Constr` constructor have been created.
563         function getInstance<
564                         Constr extends Constructor,
565                         Args extends ConstructorParameters<Constr>
566         >(constructor: Constr, ...args: Args): InstanceType<Constr> {
567                         let count = instanceCounter.get(constructor) || 0;
568
569                         const instance = new constructor(...args);
570
571                         instanceCounter.set(constructor, count + 1);
572
573                         console.log(`Created ${count + 1} instances of ${Constr.name} class`);
574
575                         return instance;
576         }
577
578
579         const idleService = getInstance(IdleService);
580         // Will log: `Created 1 instances of IdleService class`
581         const newsEntry = getInstance(News, 'New ECMAScript proposals!', 'Last month...');
582         // Will log: `Created 1 instances of News class`
583         ```
584         </details>
585
586 - [`Omit<T, K>`](https://github.com/microsoft/TypeScript/blob/71af02f7459dc812e85ac31365bfe23daf14b4e4/src/lib/es5.d.ts#L1446) â€“ Constructs a type by picking all properties from T and then removing K.
587         <details>
588         <summary>
589                         Example
590         </summary>
591
592         [Playground](https://typescript-play.js.org/?target=6#code/JYOwLgpgTgZghgYwgAgIImAWzgG2QbwChlks4BzCAVShwC5kBnMKUcgbmKYAcIFgIjBs1YgOXMpSFMWbANoBdTiW5woFddwAW0kfKWEAvoUIB6U8gDCUCHEiNkICAHdkYAJ69kz4GC3JcPG4oAHteKDABBxCYNAxsPFBIWEQUCAAPJG4wZABySUFcgJAAEzMLXNV1ck0dIuCw6EjBADpy5AB1FAQ4EGQAV0YUP2AHDy8wEOQbUugmBLwtEIA3OcmQnEjuZBgQqE7gAGtgZAhwKHdkHFGwNvGUdDIcAGUliIBJEF3kAF5kAHlML4ADyPBIAGjyBUYRQAPnkqho4NoYQA+TiEGD9EAISIhPozErQMG4AASK2gn2+AApek9pCSXm8wFSQooAJQMUkAFQAsgAZACiOAgmDOOSIJAQ+OYyGl4DgoDmf2QJRCCH6YvALQQNjsEGFovF1NyJWAy1y7OUyHMyE+yRAuFImG4Iq1YDswHxbRINjA-SgfXlHqVUE4xiAA)
593
594         ```ts
595         interface Animal {
596                         imageUrl: string;
597                         species: string;
598                         images: string[];
599                         paragraphs: string[];
600         }
601
602         // Creates new type with all properties of the `Animal` interface
603         // except 'images' and 'paragraphs' properties. We can use this
604         // type to render small hover tooltip for a wiki entry list.
605         type AnimalShortInfo = Omit<Animal, 'images' | 'paragraphs'>;
606
607         function renderAnimalHoverInfo (animals: AnimalShortInfo[]): HTMLElement {
608                         const container =  document.createElement('div');
609                         // Internal implementation.
610                         return container;
611         }
612         ```
613         </details>
614
615 You can find some examples in the [TypeScript docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#predefined-conditional-types).
616
617 ## Maintainers
618
619 - [Sindre Sorhus](https://github.com/sindresorhus)
620 - [Jarek Radosz](https://github.com/CvX)
621 - [Dimitri Benin](https://github.com/BendingBender)
622
623 ## License
624
625 (MIT OR CC0-1.0)
626
627 ---
628
629 <div align="center">
630         <b>
631                 <a href="https://tidelift.com/subscription/pkg/npm-type-fest?utm_source=npm-type-fest&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
632         </b>
633         <br>
634         <sub>
635                 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
636         </sub>
637 </div>