a489b512f41309ae19fea346d60b71dc8e717ce0a95a8073ffc7fee68db379dfa9dfc532d8e416c2b935796f476fff72fb64c0dbdf85dc208380f700144b40 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. <br>
  10. <br>
  11. <br>
  12. <div align="center">
  13. <p>
  14. <p>
  15. <sup>
  16. <a href="https://github.com/sponsors/sindresorhus">Sindre Sorhus' open source work is supported by the community</a>
  17. </sup>
  18. </p>
  19. <sup>Special thanks to:</sup>
  20. <br>
  21. <br>
  22. <a href="https://standardresume.co/tech">
  23. <img src="https://sindresorhus.com/assets/thanks/standard-resume-logo.svg" width="180"/>
  24. </a>
  25. </p>
  26. </div>
  27. <br>
  28. <hr>
  29. </div>
  30. <br>
  31. <br>
  32. [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://giphy.com/gifs/illustration-rainbow-unicorn-26AHG5KGFxSkUWw1i)
  33. [![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)
  34. 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).
  35. Either add this package as a dependency or copy-paste the needed types. No credit required. 👌
  36. PR welcome for additional commonly needed types and docs improvements. Read the [contributing guidelines](.github/contributing.md) first.
  37. ## Install
  38. ```
  39. $ npm install type-fest
  40. ```
  41. *Requires TypeScript >=3.4*
  42. ## Usage
  43. ```ts
  44. import {Except} from 'type-fest';
  45. type Foo = {
  46. unicorn: string;
  47. rainbow: boolean;
  48. };
  49. type FooWithoutRainbow = Except<Foo, 'rainbow'>;
  50. //=> {unicorn: string}
  51. ```
  52. ## API
  53. Click the type names for complete docs.
  54. ### Basic
  55. - [`Primitive`](source/basic.d.ts) - Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
  56. - [`Class`](source/basic.d.ts) - Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
  57. - [`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`.
  58. - [`JsonObject`](source/basic.d.ts) - Matches a JSON object.
  59. - [`JsonArray`](source/basic.d.ts) - Matches a JSON array.
  60. - [`JsonValue`](source/basic.d.ts) - Matches any valid JSON value.
  61. - [`ObservableLike`](source/basic.d.ts) - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
  62. ### Utilities
  63. - [`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).
  64. - [`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>`.
  65. - [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type.
  66. - [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive keys.
  67. - [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given keys.
  68. - [`RequireExactlyOne`](source/require-exactly-one.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more.
  69. - [`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.
  70. - [`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.
  71. - [`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).
  72. - [`Promisable`](source/promisable.d.ts) - Create a type that represents either the value or the value wrapped in `PromiseLike`.
  73. - [`Opaque`](source/opaque.d.ts) - Create an [opaque type](https://codemix.com/opaque-types-in-javascript/).
  74. - [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
  75. - [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required.
  76. - [`ValueOf`](source/value-of.d.ts) - Create a union of the given object's values, and optionally specify which keys to get the values from.
  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. - [`UnionToIntersection`](source/union-to-intersection.d.ts) - Convert a union type to an intersection type.
  83. - [`Stringified`](source/stringified.d.ts) - Create a type with the keys of the given type changed to `string` type.
  84. - [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length.
  85. - [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example, an array or a generator.
  86. - [`Entry`](source/entry.d.ts) - Create a type that represents the type of an entry of a collection.
  87. - [`Entries`](source/entries.d.ts) - Create a type that represents the type of the entries of a collection.
  88. - [`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.
  89. - [`Asyncify`](source/asyncify.d.ts) - Create an async version of the given function type.
  90. ### Template literal types
  91. *Note:* These require [TypeScript 4.1 or newer](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#template-literal-types).
  92. - [`CamelCase`](ts41/camel-case.d.ts) – Convert a string literal to camel-case (`fooBar`).
  93. - [`KebabCase`](ts41/kebab-case.d.ts) – Convert a string literal to kebab-case (`foo-bar`).
  94. - [`PascalCase`](ts41/pascal-case.d.ts) – Converts a string literal to pascal-case (`FooBar`)
  95. - [`SnakeCase`](ts41/snake-case.d.ts) – Convert a string literal to snake-case (`foo_bar`).
  96. - [`DelimiterCase`](ts41/delimiter-case.d.ts) – Convert a string literal to a custom string delimiter casing.
  97. - [`Get`](ts41/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.
  98. ### Miscellaneous
  99. - [`PackageJson`](source/package-json.d.ts) - Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file).
  100. - [`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).
  101. ## Declined types
  102. *If we decline a type addition, we will make sure to document the better solution here.*
  103. - [`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.
  104. - [`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.
  105. - [`SubType`](https://github.com/sindresorhus/type-fest/issues/22) - The type is powerful, but lacks good use-cases and is prone to misuse.
  106. - [`ExtractProperties` and `ExtractMethods`](https://github.com/sindresorhus/type-fest/pull/4) - The types violate the single responsibility principle. Instead, refine your types into more granular type hierarchies.
  107. ## Tips
  108. ### Built-in types
  109. There are many advanced types most users don't know about.
  110. - [`Partial<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1401-L1406) - Make all properties in `T` optional.
  111. <details>
  112. <summary>
  113. Example
  114. </summary>
  115. [Playground](https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgHIHsAmEDC6QzADmyA3gLABQyycADnanALYQBcyAzmFKEQNxUaddFDAcQAV2YAjaIMoBfKlQQAbOJ05osEAIIMAQpOBrsUMkOR1eANziRkCfISKSoD4Pg4ZseAsTIALyW1DS0DEysHADkvvoMMQA0VsKi4sgAzAAMuVaKClY2wPaOknSYDrguADwA0sgQAB6QIJjaANYQAJ7oMDp+LsQAfAAUXd0cdUnI9mo+uv6uANp1ALoAlKHhyGAAFsCcAHTOAW4eYF4gyxNrwbNwago0ypRWp66jH8QcAApwYmAjxq8SWIy2FDCNDA3ToKFBQyIdR69wmfQG1TOhShyBgomQX3w3GQE2Q6IA8jIAFYQBBgI4TTiEs5bTQYsFInrLTbbHZOIlgZDlSqQABqj0kKBC3yINx6a2xfOQwH6o2FVXFaklwSCIUkbQghBAEEwENSfNOlykEGefNe5uhB2O6sgS3GPRmLogmslG1tLxUOKgEDA7hAuydtteryAA)
  116. ```ts
  117. interface NodeConfig {
  118. appName: string;
  119. port: number;
  120. }
  121. class NodeAppBuilder {
  122. private configuration: NodeConfig = {
  123. appName: 'NodeApp',
  124. port: 3000
  125. };
  126. private updateConfig<Key extends keyof NodeConfig>(key: Key, value: NodeConfig[Key]) {
  127. this.configuration[key] = value;
  128. }
  129. config(config: Partial<NodeConfig>) {
  130. type NodeConfigKey = keyof NodeConfig;
  131. for (const key of Object.keys(config) as NodeConfigKey[]) {
  132. const updateValue = config[key];
  133. if (updateValue === undefined) {
  134. continue;
  135. }
  136. this.updateConfig(key, updateValue);
  137. }
  138. return this;
  139. }
  140. }
  141. // `Partial<NodeConfig>`` allows us to provide only a part of the
  142. // NodeConfig interface.
  143. new NodeAppBuilder().config({appName: 'ToDoApp'});
  144. ```
  145. </details>
  146. - [`Required<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1408-L1413) - Make all properties in `T` required.
  147. <details>
  148. <summary>
  149. Example
  150. </summary>
  151. [Playground](https://typescript-play.js.org/?target=6#code/AQ4SwOwFwUwJwGYEMDGNgGED21VQGJZwC2wA3gFCjXAzFJgA2A-AFzADOUckA5gNxUaIYjA4ckvGG07c+g6gF8KQkAgCuEFFDA5O6gEbEwUbLm2ESwABQIixACJIoSdgCUYAR3Vg4MACYAPGYuFvYAfACU5Ko0APRxwADKMBD+wFAAFuh2Vv7OSBlYGdmc8ABu8LHKsRyGxqY4oQT21pTCIHQMjOwA5DAAHgACxAAOjDAAdChYxL0ANLHUouKSMH0AEmAAhJhY6ozpAJ77GTCMjMCiV0ToSAb7UJPPC9WRgrEJwAAqR6MwSRQPFGUFocDgRHYxnEfGAowh-zgUCOwF6KwkUl6tXqJhCeEsxDaS1AXSYfUGI3GUxmc0WSneQA)
  152. ```ts
  153. interface ContactForm {
  154. email?: string;
  155. message?: string;
  156. }
  157. function submitContactForm(formData: Required<ContactForm>) {
  158. // Send the form data to the server.
  159. }
  160. submitContactForm({
  161. email: 'ex@mple.com',
  162. message: 'Hi! Could you tell me more about…',
  163. });
  164. // TypeScript error: missing property 'message'
  165. submitContactForm({
  166. email: 'ex@mple.com',
  167. });
  168. ```
  169. </details>
  170. - [`Readonly<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1415-L1420) - Make all properties in `T` readonly.
  171. <details>
  172. <summary>
  173. Example
  174. </summary>
  175. [Playground](https://typescript-play.js.org/?target=6#code/AQ4UwOwVwW2AZA9gc3mAbmANsA3gKFCOAHkAzMgGkOJABEwAjKZa2kAUQCcvEu32AMQCGAF2FYBIAL4BufDRABLCKLBcywgMZgEKZOoDCiCGSXI8i4hGEwwALmABnUVxXJ57YFgzZHSVF8sT1BpBSItLGEnJz1kAy5LLy0TM2RHACUwYQATEywATwAeAITjU3MAPnkrCJMXLigtUT4AClxgGztKbyDgaX99I1TzAEokr1BRAAslJwA6FIqLAF48TtswHp9MHDla9hJGACswZvmyLjAwAC8wVpm5xZHkUZDaMKIwqyWXYCW0oN4sNlsA1h0ug5gAByACyBQAggAHJHQ7ZBIFoXbzBjMCz7OoQP5YIaJNYQMAAdziCVaALGNSIAHomcAACoFJFgADKWjcSNEwG4vC4ji0wggEEQguiTnMEGALWAV1yAFp8gVgEjeFyuKICvMrCTgVxnst5jtsGC4ljsPNhXxGaAWcAAOq6YRXYDCRg+RWIcA5JSC+kWdCepQ+v3RYCU3RInzRMCGwlpC19NYBW1Ye08R1AA)
  176. ```ts
  177. enum LogLevel {
  178. Off,
  179. Debug,
  180. Error,
  181. Fatal
  182. };
  183. interface LoggerConfig {
  184. name: string;
  185. level: LogLevel;
  186. }
  187. class Logger {
  188. config: Readonly<LoggerConfig>;
  189. constructor({name, level}: LoggerConfig) {
  190. this.config = {name, level};
  191. Object.freeze(this.config);
  192. }
  193. }
  194. const config: LoggerConfig = {
  195. name: 'MyApp',
  196. level: LogLevel.Debug
  197. };
  198. const logger = new Logger(config);
  199. // TypeScript Error: cannot assign to read-only property.
  200. logger.config.level = LogLevel.Error;
  201. // We are able to edit config variable as we please.
  202. config.level = LogLevel.Error;
  203. ```
  204. </details>
  205. - [`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`.
  206. <details>
  207. <summary>
  208. Example
  209. </summary>
  210. [Playground](https://typescript-play.js.org/?target=6#code/AQ4SwOwFwUwJwGYEMDGNgEE5TCgNugN4BQoZwOUBAXMAM5RyQDmA3KeSFABYCuAtgCMISMHloMmENh04oA9tBjQJjFuzIBfYrOAB6PcADCcGElh1gEGAHcKATwAO6ebyjB5CTNlwFwSxFR0BX5HeToYABNgBDh5fm8cfBg6AHIKG3ldA2BHOOcfFNpUygJ0pAhokr4hETFUgDpswywkggAFUwA3MFtgAF5gQgowKhhVKTYKGuFRcXo1aVZgbTIoJ3RW3xhOmB6+wfbcAGsAHi3kgBpgEtGy4AAfG54BWfqAPnZm4AAlZUj4MAkMA8GAGB4vEgfMlLLw6CwPBA8PYRmMgZVgAC6CgmI4cIommQELwICh8RBgKZKvALh1ur0bHQABR5PYMui0Wk7em2ADaAF0AJS0AASABUALIAGQAogR+Mp3CROCAFBBwVC2ikBpj5CgBIqGjizLA5TAFdAmalImAuqlBRoVQh5HBgEy1eDWfs7J5cjzGYKhroVfpDEhHM4MV6GRR5NN0JrtnRg6BVirTFBeHAKYmYY6QNpdB73LmCJZBlSAXAubtvczeSmQMNSuMbmKNgBlHFgPEUNwusBIPAAQlS1xetTmxT0SDoESgdD0C4aACtHMwxytLrohawgA)
  211. ```ts
  212. interface Article {
  213. title: string;
  214. thumbnail: string;
  215. content: string;
  216. }
  217. // Creates new type out of the `Article` interface composed
  218. // from the Articles' two properties: `title` and `thumbnail`.
  219. // `ArticlePreview = {title: string; thumbnail: string}`
  220. type ArticlePreview = Pick<Article, 'title' | 'thumbnail'>;
  221. // Render a list of articles using only title and description.
  222. function renderArticlePreviews(previews: ArticlePreview[]): HTMLElement {
  223. const articles = document.createElement('div');
  224. for (const preview of previews) {
  225. // Append preview to the articles.
  226. }
  227. return articles;
  228. }
  229. const articles = renderArticlePreviews([
  230. {
  231. title: 'TypeScript tutorial!',
  232. thumbnail: '/assets/ts.jpg'
  233. }
  234. ]);
  235. ```
  236. </details>
  237. - [`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`.
  238. <details>
  239. <summary>
  240. Example
  241. </summary>
  242. [Playground](https://typescript-play.js.org/?target=6#code/AQ4ejYAUHsGcCWAXBMB2dgwGbAKYC2ADgDYwCeeemCaWArgE7ADGMxAhmuQHQBQoYEnJE8wALKEARnkaxEKdMAC8wAOS0kstGuAAfdQBM8ANzxlRjXQbVaWACwC0JPB0NqA3HwGgIwAJJoWozYHCxixnAsjAhStADmwESMMJYo1Fi4HMCIaPEu+MRklHj8gpqyoeHAAKJFFFTAAN4+giDYCIxwSAByHAR4AFw5SDF5Xm2gJBzdfQPD3WPxE5PAlBxdAPLYNQAelgh4aOHDaPQEMowrIAC+3oJ+AMKMrlrAXFhSAFZ4LEhC9g4-0BmA4JBISXgiCkBQABpILrJ5MhUGhYcATGD6Bk4Hh-jNgABrPDkOBlXyQAAq9ngYmJpOAAHcEOCRjAXqwYODfoo6DhakUSph+Uh7GI4P0xER4Cj0OSQGwMP8tP1hgAlX7swwAHgRl2RvIANALSA08ABtAC6AD4VM1Wm0Kow0MMrYaHYJjGYLLJXZb3at1HYnC43Go-QHQDcvA6-JsmEJXARgCDgMYWAhjIYhDAU+YiMAAFIwex0ZmilMITCGF79TLAGRsAgJYAAZRwSEZGzEABFTOZUrJ5Yn+jwnWgeER6HB7AAKJrADpdXqS4ZqYultTG6azVfqHswPBbtauLY7fayQ7HIbAAAMwBuAEoYw9IBq2Ixs9h2eFMOQYPQObALQKJgggABeYhghCIpikkKRpOQRIknAsZUiIeCttECBEP8NSMCkjDDAARMGziuIYxHwYOjDCMBmDNnAuTxA6irdCOBB1Lh5Dqpqn66tISIykawBnOCtqqC0gbjqc9DgpGkxegOliyfJDrRkAA)
  243. ```ts
  244. // Positions of employees in our company.
  245. type MemberPosition = 'intern' | 'developer' | 'tech-lead';
  246. // Interface describing properties of a single employee.
  247. interface Employee {
  248. firstName: string;
  249. lastName: string;
  250. yearsOfExperience: number;
  251. }
  252. // Create an object that has all possible `MemberPosition` values set as keys.
  253. // Those keys will store a collection of Employees of the same position.
  254. const team: Record<MemberPosition, Employee[]> = {
  255. intern: [],
  256. developer: [],
  257. 'tech-lead': [],
  258. };
  259. // Our team has decided to help John with his dream of becoming Software Developer.
  260. team.intern.push({
  261. firstName: 'John',
  262. lastName: 'Doe',
  263. yearsOfExperience: 0
  264. });
  265. // `Record` forces you to initialize all of the property keys.
  266. // TypeScript Error: "tech-lead" property is missing
  267. const teamEmpty: Record<MemberPosition, null> = {
  268. intern: null,
  269. developer: null,
  270. };
  271. ```
  272. </details>
  273. - [`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`.
  274. <details>
  275. <summary>
  276. Example
  277. </summary>
  278. [Playground](https://typescript-play.js.org/?target=6#code/JYOwLgpgTgZghgYwgAgMrQG7QMIHsQzADmyA3gFDLIAOuUYAXMiAK4A2byAPsgM5hRQJHqwC2AI2gBucgF9y5MAE9qKAEoQAjiwj8AEnBAATNtGQBeZAAooWphu26wAGmS3e93bRC8IASgsAPmRDJRlyAHoI5ABRAA8ENhYjFFYOZGVVZBgoXFFkAAM0zh5+QRBhZhYJaAKAOkjogEkQZAQ4X2QAdwALCFbaemRgXmQtFjhOMFwq9K6ULuB0lk6U+HYwZAxJnQaYFhAEMGB8ZCIIMAAFOjAANR2IK0HGWISklIAedCgsKDwCYgAbQA5M9gQBdVzFQJ+JhiSRQMiUYYwayZCC4VHPCzmSzAspCYEBWxgFhQAZwKC+FpgJ43VwARgADH4ZFQSWSBjcZPJyPtDsdTvxKWBvr8rD1DCZoJ5HPopaYoK4EPhCEQmGKcKriLCtrhgEYkVQVT5Nr4fmZLLZtMBbFZgT0wGBqES6ghbHBIJqoBKFdBWQpjfh+DQbhY2tqiHVsbjLMVkAB+ZAAZiZaeQTHOVxu9ySjxNaujNwDVHNvzqbBGkBAdPoAfkQA)
  279. ```ts
  280. interface ServerConfig {
  281. port: null | string | number;
  282. }
  283. type RequestHandler = (request: Request, response: Response) => void;
  284. // Exclude `null` type from `null | string | number`.
  285. // In case the port is equal to `null`, we will use default value.
  286. function getPortValue(port: Exclude<ServerConfig['port'], null>): number {
  287. if (typeof port === 'string') {
  288. return parseInt(port, 10);
  289. }
  290. return port;
  291. }
  292. function startServer(handler: RequestHandler, config: ServerConfig): void {
  293. const server = require('http').createServer(handler);
  294. const port = config.port === null ? 3000 : getPortValue(config.port);
  295. server.listen(port);
  296. }
  297. ```
  298. </details>
  299. - [`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`.
  300. <details>
  301. <summary>
  302. Example
  303. </summary>
  304. [Playground](https://typescript-play.js.org/?target=6#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXzSwEdkQBJYACgEoAueVZAWwCMQYBuAKDDwGcM8MgBF4AXngBlAJ6scESgHIRi6ty5ZUGdoihgEABXZ888AN5d48ANoiAuvUat23K6ihMQ9ATE0BzV3goPy8GZjZOLgBfLi4Aejj4AEEICBwAdz54MAALKFQQ+BxEeAAHY1NgKAwoIKy0grr4DByEUpgccpgMaXgAaxBerCzi+B9-ZulygDouFHRsU1z8kKMYE1RhaqgAHkt4AHkWACt4EAAPbVRgLLWNgBp9gGlBs8uQa6yAUUuYPQwdgNpKM7nh7mMML4CgA+R5WABqUAgpDeVxuhxO1he0jsXGh8EoOBO9COx3BQPo2PBADckaR6IjkSA6PBqTgsMBzPsicdrEC7OJWXSQNwYvFEgAVTS9JLXODpeDpKBZFg4GCoWa8VACIJykAKiQWKy2YQOAioYikCg0OEMDyhRSy4DyxS24KhAAMjyi6gS8AAwjh5OD0iBFHAkJoEOksC1mnkMJq8gUQKDNttKPlnfrwYp3J5XfBHXqoKpfYkAOI4ansTxaeDADmoRSCCBYAbxhC6TDx6rwYHIRX5bScjA4bLJwoDmDwDkfbA9JMrVMVdM1TN69LgkTgwgkchUahqIA)
  305. ```ts
  306. declare function uniqueId(): number;
  307. const ID = Symbol('ID');
  308. interface Person {
  309. [ID]: number;
  310. name: string;
  311. age: number;
  312. }
  313. // Allows changing the person data as long as the property key is of string type.
  314. function changePersonData<
  315. Obj extends Person,
  316. Key extends Extract<keyof Person, string>,
  317. Value extends Obj[Key]
  318. > (obj: Obj, key: Key, value: Value): void {
  319. obj[key] = value;
  320. }
  321. // Tiny Andrew was born.
  322. const andrew = {
  323. [ID]: uniqueId(),
  324. name: 'Andrew',
  325. age: 0,
  326. };
  327. // Cool, we're fine with that.
  328. changePersonData(andrew, 'name', 'Pony');
  329. // Goverment didn't like the fact that you wanted to change your identity.
  330. changePersonData(andrew, ID, uniqueId());
  331. ```
  332. </details>
  333. - [`NonNullable<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1446-L1449) - Exclude `null` and `undefined` from `T`.
  334. <details>
  335. <summary>
  336. Example
  337. </summary>
  338. 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>)
  339. [Playground](https://typescript-play.js.org/?target=6#code/C4TwDgpgBACg9gJ2AOQK4FsBGEFQLxQDOwCAlgHYDmUAPlORtrnQwDasDcAUFwPQBU-WAEMkUOADMowqAGNWwwoSgATCBIqlgpOOSjAAFsOBRSy1IQgr9cKJlSlW1mZYQA3HFH68u8xcoBlHA8EACEHJ08Aby4oKDBUTFZSWXjEFEYcAEIALihkXTR2YSSIAB54JDQsHAA+blj4xOTUsHSACkMzPKD3HHDHNQQAGjSkPMqMmoQASh7g-oihqBi4uNIpdraxPAI2VhmVxrX9AzMAOm2ppnwoAA4ABifuE4BfKAhWSyOTuK7CS7pao3AhXF5rV48E4ICDAVAIPT-cGQyG+XTEIgLMJLTx7CAAdygvRCA0iCHaMwarhJOIQjUBSHaACJHk8mYdeLwxtdcVAAOSsh58+lXdr7Dlcq7A3n3J4PEUdADMcspUE53OluAIUGVTx46oAKuAIAFZGQwCYAKIIBCILjUxaDHAMnla+iodjcIA)
  340. ```ts
  341. type PortNumber = string | number | null;
  342. /** Part of a class definition that is used to build a server */
  343. class ServerBuilder {
  344. portNumber!: NonNullable<PortNumber>;
  345. port(this: ServerBuilder, port: PortNumber): ServerBuilder {
  346. if (port == null) {
  347. this.portNumber = 8000;
  348. } else {
  349. this.portNumber = port;
  350. }
  351. return this;
  352. }
  353. }
  354. const serverBuilder = new ServerBuilder();
  355. serverBuilder
  356. .port('8000') // portNumber = '8000'
  357. .port(null) // portNumber = 8000
  358. .port(3000); // portNumber = 3000
  359. // TypeScript error
  360. serverBuilder.portNumber = null;
  361. ```
  362. </details>
  363. - [`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.
  364. <details>
  365. <summary>
  366. Example
  367. </summary>
  368. [Playground](https://typescript-play.js.org/?target=6#code/GYVwdgxgLglg9mABAZwBYmMANgUwBQxgAOIUAXIgIZgCeA2gLoCUFAbnDACaIDeAUIkQB6IYgCypSlBxUATrMo1ECsJzgBbLEoipqAc0J7EMKMgDkiHLnU4wp46pwAPHMgB0fAL58+oSLARECEosLAA5ABUYG2QAHgAxJGdpVWREPDdMylk9ZApqemZEAF4APipacrw-CApEgBogkKwAYThwckQwEHUAIxxZJl4BYVEImiIZKF0oZRwiWVdbeygJmThgOYgcGFYcbhqApCJsyhtpWXcR1cnEePBoeDAABVPzgbTixFeFd8uEsClADcIxGiygIFkSEOT3SmTc2VydQeRx+ZxwF2QQ34gkEwDgsnSuFmMBKiAADEDjIhYk1Qm0OlSYABqZnYka4xA1DJZHJYkGc7yCbyeRA+CAIZCzNAYbA4CIAdxg2zJwVCkWirjwMswuEaACYmCCgA)
  369. ```ts
  370. function shuffle(input: any[]): void {
  371. // Mutate array randomly changing its' elements indexes.
  372. }
  373. function callNTimes<Fn extends (...args: any[]) => any> (func: Fn, callCount: number) {
  374. // Type that represents the type of the received function parameters.
  375. type FunctionParameters = Parameters<Fn>;
  376. return function (...args: FunctionParameters) {
  377. for (let i = 0; i < callCount; i++) {
  378. func(...args);
  379. }
  380. }
  381. }
  382. const shuffleTwice = callNTimes(shuffle, 2);
  383. ```
  384. </details>
  385. - [`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.
  386. <details>
  387. <summary>
  388. Example
  389. </summary>
  390. [Playground](https://typescript-play.js.org/?target=6#code/MYGwhgzhAECCBOAXAlqApgWQPYBM0mgG8AoaaFRENALmgkXmQDsBzAblOmCycTV4D8teo1YdO3JiICuwRFngAKClWENmLAJRFOZRAAtkEAHQq00ALzlklNBzIBfYk+KhIMAJJTEYJsDQAwmDA+mgAPAAq0GgAHnxMODCKTGgA7tCKxllg8CwQtL4AngDaALraFgB80EWa1SRkAA6MAG5gfNAB4FABPDJyCrQR9tDNyG0dwMGhtBhgjWEiGgA00F70vv4RhY3hEZXVVinpc42KmuJkkv3y8Bly8EPaDWTkhiZd7r3e8LK3llwGCMXGQWGhEOsfH5zJlsrl8p0+gw-goAAo5MAAW3BaHgEEilU0tEhmzQ212BJ0ry4SOg+kg+gBBiMximIGA0nAfAQLGk2N4EAAEgzYcYcnkLsRdDTvNEYkYUKwSdCme9WdM0MYwYhFPSIPpJdTkAAzDKxBUaZX+aAAQgsVmkCTQxuYaBw2ng4Ok8CYcotSu8pMur09iG9vuObxZnx6SN+AyUWTF8MN0CcZE4Ywm5jZHK5aB5fP4iCFIqT4oRRTKRLo6lYVNeAHpG50wOzOe1zHr9NLQ+HoABybsD4HOKXXRA1JCoKhBELmI5pNaB6Fz0KKBAodDYPAgSUTmqYsAALx4m5nC6nW9nGq14KtaEUA9gR9PvuNCjQ9BgACNvcwNBtAcLiAA)
  391. ```ts
  392. class ArticleModel {
  393. title: string;
  394. content?: string;
  395. constructor(title: string) {
  396. this.title = title;
  397. }
  398. }
  399. class InstanceCache<T extends (new (...args: any[]) => any)> {
  400. private ClassConstructor: T;
  401. private cache: Map<string, InstanceType<T>> = new Map();
  402. constructor (ctr: T) {
  403. this.ClassConstructor = ctr;
  404. }
  405. getInstance (...args: ConstructorParameters<T>): InstanceType<T> {
  406. const hash = this.calculateArgumentsHash(...args);
  407. const existingInstance = this.cache.get(hash);
  408. if (existingInstance !== undefined) {
  409. return existingInstance;
  410. }
  411. return new this.ClassConstructor(...args);
  412. }
  413. private calculateArgumentsHash(...args: any[]): string {
  414. // Calculate hash.
  415. return 'hash';
  416. }
  417. }
  418. const articleCache = new InstanceCache(ArticleModel);
  419. const amazonArticle = articleCache.getInstance('Amazon forests burining!');
  420. ```
  421. </details>
  422. - [`ReturnType<T>`](https://github.com/Microsoft/TypeScript/blob/2961bc3fc0ea1117d4e53bc8e97fa76119bc33e3/src/lib/es5.d.ts#L1461-L1464) – Obtain the return type of a function type.
  423. <details>
  424. <summary>
  425. Example
  426. </summary>
  427. [Playground](https://typescript-play.js.org/?target=6#code/MYGwhgzhAECSAmICmBlJAnAbgS2E6A3gFDTTwD2AcuQC4AW2AdgOYAUAlAFzSbnbyEAvkWFFQkGJSQB3GMVI1sNZNwg10TZgG4S0YOUY0kh1es07d+xmvQBXYDXLpWi5UlMaWAGj0GjJ6BtNdkJdBQYIADpXZGgAXmgYpB1ScOwoq38aeN9DYxoU6GFRKzVoJjUwRjwAYXJbPPRuAFkwAAcAHgAxBodsAx9GWwBbACMMAD4cxhloVraOCyYjdAAzMDxoOut1e0d0UNIZ6WhWSPOwdGYIbiqATwBtAF0uaHudUQB6ACpv6ABpJBINqJdAbADW0Do5BOw3u5R2VTwMHIq2gAANtjZ0bkbHsnFCwJh8ONjHp0EgwEZ4JFoN9PkRVr1FAZoMwkDRYIjqkgOrosepoEgAB7+eAwAV2BxOLy6ACCVxgIrFEoMeOl6AACpcwMMORgIB1JRMiBNWKVdhruJKfOdIpdrtwFddXlzKjyACp3Nq842HaDIbL6BrZBIVGhIpB1EMYSLsmjmtWW-YhAA+qegAAYLKQLQj3ZsEsdccmnGcLor2Dn8xGedHGpEIBzEzspfsfMHDNAANTQACMVaIljV5GQkRA5DYmIpVKQAgAJARO9le33BDXIyi0YuLW2nJFGLqkOvxFB0YPdBSaLZ0IwNzyPkO8-xkGgsLh8Al427a3hWAhXwwHA8EHT5PmgAB1bAQBAANJ24adKWpft72RaBUTgRBUCAj89HAM8xCTaBjggABRQx0DuHJv25P9dCkWRZVIAAiBjoFImpmjlFBgA0NpsjadByDacgIDAEAIAAQmYpjoGYgAZSBsmGPw6DtZiiFA8CoJguDmAQmoZ2QvtUKQLdoAYmBTwgdEiCAA)
  428. ```ts
  429. /** Provides every element of the iterable `iter` into the `callback` function and stores the results in an array. */
  430. function mapIter<
  431. Elem,
  432. Func extends (elem: Elem) => any,
  433. Ret extends ReturnType<Func>
  434. >(iter: Iterable<Elem>, callback: Func): Ret[] {
  435. const mapped: Ret[] = [];
  436. for (const elem of iter) {
  437. mapped.push(callback(elem));
  438. }
  439. return mapped;
  440. }
  441. const setObject: Set<string> = new Set();
  442. const mapObject: Map<number, string> = new Map();
  443. mapIter(setObject, (value: string) => value.indexOf('Foo')); // number[]
  444. mapIter(mapObject, ([key, value]: [number, string]) => {
  445. return key % 2 === 0 ? value : 'Odd';
  446. }); // string[]
  447. ```
  448. </details>
  449. - [`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.
  450. <details>
  451. <summary>
  452. Example
  453. </summary>
  454. [Playground](https://typescript-play.js.org/?target=6#code/MYGwhgzhAECSAmICmBlJAnAbgS2E6A3gFDTTwD2AcuQC4AW2AdgOYAUAlAFzSbnbyEAvkWFFQkGJSQB3GMVI1sNZNwg10TZgG4S0YOUY0kh1es07d+xmvQBXYDXLpWi5UlMaWAGj0GjJ6BtNdkJdBQYIADpXZGgAXmgYpB1ScOwoq38aeN9DYxoU6GFRKzVoJjUwRjwAYXJbPPRuAFkwAAcAHgAxBodsAx9GWwBbACMMAD4cxhloVraOCyYjdAAzMDxoOut1e0d0UNIZ6WhWSPOwdGYIbiqATwBtAF0uaHudUQB6ACpv6ABpJBINqJdAbADW0Do5BOw3u5R2VTwMHIq2gAANtjZ0bkbHsnFCwJh8ONjHp0EgwEZ4JFoN9PkRVr1FAZoMwkDRYIjqkgOrosepoEgAB7+eAwAV2BxOLy6ACCVxgIrFEoMeOl6AACpcwMMORgIB1JRMiBNWKVdhruJKfOdIpdrtwFddXlzKjyACp3Nq842HaDIbL6BrZBIVGhIpB1EMYSLsmjmtWW-YhAA+qegAAYLKQLQj3ZsEsdccmnGcLor2Dn8xGedHGpEIBzEzspfsfMHDNAANTQACMVaIljV5GQkRA5DYmIpVKQAgAJARO9le33BDXIyi0YuLW2nJFGLqkOvxFB0YPdBSaLZ0IwNzyPkO8-xkGgsLh8Al427a3hWAhXwwHA8EHT5PmgAB1bAQBAANJ24adKWpft72RaBUTgRBUCAj89HAM8xCTaBjggABRQx0DuHJv25P9dCkWRZVIAAiBjoFImpmjlFBgA0NpsjadByDacgIDAEAIAAQmYpjoGYgAZSBsmGPw6DtZiiFA8CoJguDmAQmoZ2QvtUKQLdoAYmBTwgdEiCAA)
  455. ```ts
  456. class IdleService {
  457. doNothing (): void {}
  458. }
  459. class News {
  460. title: string;
  461. content: string;
  462. constructor(title: string, content: string) {
  463. this.title = title;
  464. this.content = content;
  465. }
  466. }
  467. const instanceCounter: Map<Function, number> = new Map();
  468. interface Constructor {
  469. new(...args: any[]): any;
  470. }
  471. // Keep track how many instances of `Constr` constructor have been created.
  472. function getInstance<
  473. Constr extends Constructor,
  474. Args extends ConstructorParameters<Constr>
  475. >(constructor: Constr, ...args: Args): InstanceType<Constr> {
  476. let count = instanceCounter.get(constructor) || 0;
  477. const instance = new constructor(...args);
  478. instanceCounter.set(constructor, count + 1);
  479. console.log(`Created ${count + 1} instances of ${Constr.name} class`);
  480. return instance;
  481. }
  482. const idleService = getInstance(IdleService);
  483. // Will log: `Created 1 instances of IdleService class`
  484. const newsEntry = getInstance(News, 'New ECMAScript proposals!', 'Last month...');
  485. // Will log: `Created 1 instances of News class`
  486. ```
  487. </details>
  488. - [`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.
  489. <details>
  490. <summary>
  491. Example
  492. </summary>
  493. [Playground](https://typescript-play.js.org/?target=6#code/JYOwLgpgTgZghgYwgAgIImAWzgG2QbwChlks4BzCAVShwC5kBnMKUcgbmKYAcIFgIjBs1YgOXMpSFMWbANoBdTiW5woFddwAW0kfKWEAvoUIB6U8gDCUCHEiNkICAHdkYAJ69kz4GC3JcPG4oAHteKDABBxCYNAxsPFBIWEQUCAAPJG4wZABySUFcgJAAEzMLXNV1ck0dIuCw6EjBADpy5AB1FAQ4EGQAV0YUP2AHDy8wEOQbUugmBLwtEIA3OcmQnEjuZBgQqE7gAGtgZAhwKHdkHFGwNvGUdDIcAGUliIBJEF3kAF5kAHlML4ADyPBIAGjyBUYRQAPnkqho4NoYQA+TiEGD9EAISIhPozErQMG4AASK2gn2+AApek9pCSXm8wFSQooAJQMUkAFQAsgAZACiOAgmDOOSIJAQ+OYyGl4DgoDmf2QJRCCH6YvALQQNjsEGFovF1NyJWAy1y7OUyHMyE+yRAuFImG4Iq1YDswHxbRINjA-SgfXlHqVUE4xiAA)
  494. ```ts
  495. interface Animal {
  496. imageUrl: string;
  497. species: string;
  498. images: string[];
  499. paragraphs: string[];
  500. }
  501. // Creates new type with all properties of the `Animal` interface
  502. // except 'images' and 'paragraphs' properties. We can use this
  503. // type to render small hover tooltip for a wiki entry list.
  504. type AnimalShortInfo = Omit<Animal, 'images' | 'paragraphs'>;
  505. function renderAnimalHoverInfo (animals: AnimalShortInfo[]): HTMLElement {
  506. const container = document.createElement('div');
  507. // Internal implementation.
  508. return container;
  509. }
  510. ```
  511. </details>
  512. - [`Uppercase<S extends string>`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-1.html#template-literal-types) - Transforms every character in a string into uppercase.
  513. <details>
  514. <summary>
  515. Example
  516. </summary>
  517. ```ts
  518. type T = Uppercase<'hello'>; // 'HELLO'
  519. type T2 = Uppercase<'foo' | 'bar'>; // 'FOO' | 'BAR'
  520. type T3<S extends string> = Uppercase<`aB${S}`>;
  521. type T4 = T30<'xYz'>; // 'ABXYZ'
  522. type T5 = Uppercase<string>; // string
  523. type T6 = Uppercase<any>; // any
  524. type T7 = Uppercase<never>; // never
  525. type T8 = Uppercase<42>; // Error, type 'number' does not satisfy the constraint 'string'
  526. ```
  527. </details>
  528. - [`Lowercase<S extends string>`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-1.html#template-literal-types) - Transforms every character in a string into lowercase.
  529. <details>
  530. <summary>
  531. Example
  532. </summary>
  533. ```ts
  534. type T = Lowercase<'HELLO'>; // 'hello'
  535. type T2 = Lowercase<'FOO' | 'BAR'>; // 'foo' | 'bar'
  536. type T3<S extends string> = Lowercase<`aB${S}`>;
  537. type T4 = T32<'xYz'>; // 'abxyz'
  538. type T5 = Lowercase<string>; // string
  539. type T6 = Lowercase<any>; // any
  540. type T7 = Lowercase<never>; // never
  541. type T8 = Lowercase<42>; // Error, type 'number' does not satisfy the constraint 'string'
  542. ```
  543. </details>
  544. - [`Capitalize<S extends string>`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-1.html#template-literal-types) - Transforms the first character in a string into uppercase.
  545. <details>
  546. <summary>
  547. Example
  548. </summary>
  549. ```ts
  550. type T = Capitalize<'hello'>; // 'Hello'
  551. type T2 = Capitalize<'foo' | 'bar'>; // 'Foo' | 'Bar'
  552. type T3<S extends string> = Capitalize<`aB${S}`>;
  553. type T4 = T32<'xYz'>; // 'ABxYz'
  554. type T5 = Capitalize<string>; // string
  555. type T6 = Capitalize<any>; // any
  556. type T7 = Capitalize<never>; // never
  557. type T8 = Capitalize<42>; // Error, type 'number' does not satisfy the constraint 'string'
  558. ```
  559. </details>
  560. - [`Uncapitalize<S extends string>`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-1.html#template-literal-types) - Transforms the first character in a string into lowercase.
  561. <details>
  562. <summary>
  563. Example
  564. </summary>
  565. ```ts
  566. type T = Uncapitalize<'Hello'>; // 'hello'
  567. type T2 = Uncapitalize<'Foo' | 'Bar'>; // 'foo' | 'bar'
  568. type T3<S extends string> = Uncapitalize<`AB${S}`>;
  569. type T4 = T30<'xYz'>; // 'aBxYz'
  570. type T5 = Uncapitalize<string>; // string
  571. type T6 = Uncapitalize<any>; // any
  572. type T7 = Uncapitalize<never>; // never
  573. type T8 = Uncapitalize<42>; // Error, type 'number' does not satisfy the constraint 'string'
  574. ```
  575. </details>
  576. You can find some examples in the [TypeScript docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#predefined-conditional-types).
  577. ## Maintainers
  578. - [Sindre Sorhus](https://github.com/sindresorhus)
  579. - [Jarek Radosz](https://github.com/CvX)
  580. - [Dimitri Benin](https://github.com/BendingBender)
  581. - [Pelle Wessman](https://github.com/voxpelli)
  582. ## License
  583. (MIT OR CC0-1.0)
  584. ---
  585. <div align="center">
  586. <b>
  587. <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>
  588. </b>
  589. <br>
  590. <sub>
  591. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  592. </sub>
  593. </div>