{"name":"ts-essentials","description":"All essential Typescript types in one place","keywords":["typescript","types","essentials"],"version":"3.0.0-beta.0","main":"dist/index.js","types":"dist/index.d.ts","repository":{"type":"git","url":"git+ssh://git@github.com/krzkaczor/ts-essentials.git"},"author":{"name":"Krzysztof Kaczor","email":"chris@kaczor.io"},"license":"MIT","scripts":{"build":"rm -rf ./dist && tsc -p tsconfig.prod.json --outDir ./dist","prepublishOnly":"yarn test && yarn build","test":"prettier -c **/*.ts && tsc --noEmit","test:fix":"prettier --write **/*.ts && tsc --noEmit"},"devDependencies":{"@codechecks/build-size-watcher":"^0.1.0","@codechecks/client":"^0.1.1-beta.0","conditional-type-checks":"^1.0.1","prettier":"^1.18.2","typescript":"^3.5.2"},"readme":"<p align=\"center\">\n  <img src=\"https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/google/146/toolbox_1f9f0.png\" width=\"120\" alt=\"ts-essentials\">\n  <h3 align=\"center\">ts-essentials</h3>\n  <p align=\"center\">All essential TypeScript types in one place 🤙</p>\n  <p align=\"center\">\n    <img alt=\"Downloads\" src=\"https://img.shields.io/npm/dm/ts-essentials.svg\">\n    <img alt=\"Build status\" src=\"https://circleci.com/gh/krzkaczor/ts-essentials.svg?style=svg\">\n    <a href=\"/package.json\"><img alt=\"Software License\" src=\"https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\"></a>\n    <img src=\"https://img.shields.io/badge/all_contributors-12-orange.svg?style=flat-square\" alt=\"All Contributors\">\n    <img src=\"https://raw.githubusercontent.com/codechecks/docs/master/images/badges/badge-default.svg?sanitize=true\" alt=\"codechecks.io\">\n  </p>\n</p>\n\n## Install\n\n```sh\nnpm add --save-dev ts-essentials\n```\n\nor\n\n```sh\nyarn add --dev ts-essentials\n```\n\n## What's inside?\n\n- [Install](#Install)\n- [What's inside?](#Whats-inside)\n  - [Basic:](#Basic)\n  - [Dictionaries](#Dictionaries)\n  - [Deep Partial & Deep Required & Deep Readonly & Deep NonNullable](#Deep-Partial--Deep-Required--Deep-Readonly--Deep-NonNullable)\n  - [Writable](#Writable)\n  - [Omit](#Omit)\n  - [OmitProperties](#OmitProperties)\n  - [NonNever](#NonNever)\n  - [Merge](#Merge)\n  - [MarkRequired](#MarkRequired)\n  - [ReadonlyKeys](#ReadonlyKeys)\n  - [WritableKeys](#WritableKeys)\n  - [UnionToIntersection](#UnionToIntersection)\n  - [Opaque types](#Opaque-types)\n  - [Tuple constraint](#Tuple-constraint)\n  - [Literal types](#Literal-types)\n  - [Exhaustive switch cases](#Exhaustive-switch-cases)\n  - [ValueOf type](#ValueOf-type)\n  - [AsyncOrSync type](#AsyncOrSync-type)\n- [Contributors](#Contributors)\n\n### Basic:\n\n- `Primitive` type matching all primitive values.\n\n### Dictionaries\n\n```typescript\nconst stringDict: Dictionary<string> = {\n  a: \"A\",\n  b: \"B\",\n};\n\n// Specify second type argument to change dictionary keys type\nconst dictOfNumbers: Dictionary<string, number> = {\n  420: \"four twenty\",\n  1337: \"HAX\",\n};\n\n// You may specify union types as key to cover all possible cases. It acts the same as Record from TS's standard library\nexport type DummyOptions = \"open\" | \"closed\" | \"unknown\";\nconst dictFromUnionType: Dictionary<number, DummyOptions> = {\n  closed: 1,\n  open: 2,\n  unknown: 3,\n};\n\n// and get dictionary values\ntype stringDictValues = DictionaryValues<typeof stringDict>;\n// Result: string\n```\n\n### Deep Partial & Deep Required & Deep Readonly & Deep NonNullable\n\n```typescript\ntype ComplexObject = {\n  simple: number;\n  nested: {\n    a: string;\n    array: [{ bar: number }];\n  };\n};\n\ntype ComplexObjectPartial = DeepPartial<ComplexObject>;\nconst samplePartial: ComplexObjectPartial = {\n  nested: {\n    array: [{}],\n  },\n};\n\ntype ComplexObjectAgain = DeepRequired<ComplexObjectPartial>;\nconst sampleRequired: ComplexObjectAgain = {\n  simple: 5,\n  nested: {\n    a: \"test\",\n    array: [{bar: 1}],\n  },\n};\n\ntype ComplexObjectReadonly = DeepReadonly<ComplexObject>;\n\ntype ComplexNullableObject = {\n  simple: number | null | undefined;\n  nested: {\n    a: string | null | undefined;\n    array: [{ bar: number | null | undefined }] | null | undefined;\n  };\n};\n\ntype ComplexObjectNonNullable = DeepNonNullable<ComplexNullableObject>;\nconst sampleNonNullable: ComplexObjectNonNullable = {\n  simple: 5,\n  nested: {\n    a: \"test\",\n    array: [{bar: null}], // Error: Type 'null' is not assignable to type 'number'\n  }\n}\n```\n\n### Writable\n\nMake all attributes of object writable.\n\n```typescript\ntype Foo = {\n  readonly a: number;\n  readonly b: string;\n};\n\nconst foo: Foo = ({ a: 1, b: \"b\" }(foo as Writable<typeof foo>).a = 42);\n```\n\n```typescript\ntype Foo = {\n  readonly foo: string;\n  bar: {\n    readonly x: number;\n  };\n}[];\n\nconst test: DeepWritable<Foo> = [\n  {\n    foo: \"a\",\n    bar: {\n      x: 5,\n    },\n  },\n];\n\n// we can freely write to this object\ntest[0].foo = \"b\";\ntest[0].bar.x = 2;\n```\n\n### Omit\n\nNOTE: Builtin `Omit` became part of TypeScript 3.5\n\n```typescript\ntype ComplexObject = {\n  simple: number;\n  nested: {\n    a: string;\n    array: [{ bar: number }];\n  };\n};\n\ntype SimplifiedComplexObject = Omit<ComplexObject, \"nested\">;\n\n// Result:\n// {\n//  simple: number\n// }\n\n// if you want to Omit multiple properties just use union type:\ntype SimplifiedComplexObject = Omit<ComplexObject, \"nested\" | \"simple\">;\n\n// Result:\n// { } (empty type)\n```\n\n### OmitProperties\n\nRemoves all properties extending type `P` in type `T`.\n\n```typescript\ninterface Example {\n  log(): void;\n  version: string;\n}\n\ntype ExampleWithoutMethods = OmitProperties<Example, Function>;\n\n// Result:\n// {\n//   version: string;\n// }\n\n// if you want to Omit multiple properties just use union type like\n\ntype ExampleWithoutMethods = OmitProperties<Example, Function | string>;\n// Result:\n// { } (empty type)\n\n```\n\n### NonNever\n\nUseful for purifying object types. It improves intellisense but also allows for extracting keys satisfying a conditional\ntype.\n\n```typescript\ntype GetDefined<TypesMap extends { [key: string]: any }> = keyof NonNever<\n  { [T in keyof TypesMap]: TypesMap[T] extends undefined ? never : TypesMap[T] }\n>;\n```\n\n### Merge\n\n```typescript\ntype Foo = {\n  a: number;\n  b: string;\n};\n\ntype Bar = {\n  b: number;\n};\n\nconst xyz: Merge<Foo, Bar> = { a: 4, b: 2 };\n// Result:\n// {\n//   a: number,\n//   b: number,\n// }\n```\n\n### MarkRequired\n\nUseful when you're sure some optional properties will be set. A real life example: when selecting\nan object with its related entities from an ORM.\n\n```typescript\nclass User {\n  id: number;\n  posts?: Post[];\n  photos?: Photo[];\n}\ntype UserWithPosts = MarkRequired<User, 'posts'>;\n\n// example usage with a TypeORM repository -- `posts` are now required, `photos` are still optional\nasync function getUserWithPosts(id: number): Promise<UserWithPosts> {\n  return userRepo.findOneOrFail({ id }, { relations: ['posts'] }) as Promise<UserWithPosts>;\n}\n```\n\n### ReadonlyKeys\n\nGets keys of an object which are readonly.\n\n```typescript\ntype T = {\n  readonly a: number;\n  b: string;\n};\ntype Result = ReadonlyKeys<T>\n// Result:\n// \"a\"\n```\n\n### WritableKeys\n\nGets keys of an object which are writable.\n\n```typescript\ntype T = {\n  readonly a: number;\n  b: string;\n};\ntype Result = WritableKeys<T>\n// Result:\n// \"b\"\n```\n\n### UnionToIntersection\n\nUseful for converting mapped types with function values to intersection type (so in this case - overloaded function).\n\n```typescript\ntype Foo = {\n  bar: string;\n  xyz: number;\n};\n\ntype Fn = UnionToIntersection<{ [K in keyof Foo]: (type: K, arg: Foo[K]) => any }[keyof Foo]>;\n```\n\n### Opaque types\n\n```typescript\ntype PositiveNumber = Opaque<number, \"positive-number\">;\n\nfunction makePositiveNumber(n: number): PositiveNumber {\n  if (n <= 0) {\n    throw new Error(\"Value not positive !!!\");\n  }\n  return (n as any) as PositiveNumber; // this ugly cast is required but only when \"producing\" opaque types\n}\n```\n\n### Tuple constraint\n\n```typescript\nfunction foo<T extends Tuple>(tuple: T): T {\n  return tuple;\n}\n\nconst ret = foo([\"s\", 1]);\n// return type of [string, number]\n```\n\nYou can also parametrize `Tuple` type with a type argument to constraint it to certain types, i.e.\n`Tuple<string | number>`.\n\n### Literal types\n\n_For TypeScript >= 3.4_: TypeScript 3.4 shipped\n[`const` assertions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html) which are very\nsimilar to our `literal` helper but also make type readonly, you should prefer `as const` construct.\n\n_For TypeScript < 3.4_: this is served as a backport of the [`const` assertions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html) added since TypeScript 3.4.\n\n```typescript\n// prevent type widening https://blog.mariusschulz.com/2017/02/04/typescript-2-1-literal-type-widening\nconst t = {\n  letter: literal(\"a\"), // type stays \"a\" not string\n  digit: literal(5), // type stays 5 not number\n};\n```\n\n### Exhaustive switch cases\n\n```typescript\nfunction actOnDummyOptions(options: DummyOptions): string {\n  switch (options) {\n    case \"open\":\n      return \"it's open!\";\n    case \"closed\":\n      return \"it's closed\";\n    case \"unknown\":\n      return \"i have no idea\";\n    default:\n      // if you would add another option to DummyOptions, you'll get error here!\n      throw new UnreachableCaseError(options);\n  }\n}\n```\n\n### ValueOf type\n\n```typescript\nconst obj = {\n  id: \"123e4567-e89b-12d3-a456-426655440000\",\n  name: \"Test object\",\n  timestamp: 1548768231486,\n};\n\ntype objKeys = ValueOf<typeof obj>;\n// Result: string | number\n```\n\n### AsyncOrSync type\n\nUseful as a return type in interfaces or abstract classes with missing implementation\n\n```typescript\ninterface CiProvider {\n  getSHA(): AsyncOrSync<string>;\n  // same as\n  getSHA(): Promise<string> | string;\n}\n\nclass Circle implements CiProvider {\n  // implementation can use sync version\n  getSHA() {\n    return \"abc\";\n  }\n}\n\nclass Travis implements CiProvider {\n  // implementation can use async version when needed\n  async getSHA() {\n    // do async call\n    return \"def\";\n  }\n}\n```\n\n## Contributors\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->\n<!-- prettier-ignore -->\n<table><tr><td align=\"center\"><a href=\"https://twitter.com/krzkaczor\"><img src=\"https://avatars2.githubusercontent.com/u/1814312?v=4\" width=\"100px;\" alt=\"Chris Kaczor\"/><br /><sub><b>Chris Kaczor</b></sub></a><br /><a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=krzkaczor\" title=\"Code\">💻</a> <a href=\"#business-krzkaczor\" title=\"Business development\">💼</a> <a href=\"#example-krzkaczor\" title=\"Examples\">💡</a> <a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=krzkaczor\" title=\"Documentation\">📖</a></td><td align=\"center\"><a href=\"https://scholar.google.com/citations?user=3xZtvpAAAAAJ\"><img src=\"https://avatars3.githubusercontent.com/u/9780746?v=4\" width=\"100px;\" alt=\"Xiao Liang\"/><br /><sub><b>Xiao Liang</b></sub></a><br /><a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=yxliang01\" title=\"Code\">💻</a> <a href=\"#ideas-yxliang01\" title=\"Ideas, Planning, & Feedback\">🤔</a> <a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=yxliang01\" title=\"Documentation\">📖</a></td><td align=\"center\"><a href=\"https://github.com/Andarist\"><img src=\"https://avatars2.githubusercontent.com/u/9800850?v=4\" width=\"100px;\" alt=\"Mateusz Burzyński\"/><br /><sub><b>Mateusz Burzyński</b></sub></a><br /><a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=Andarist\" title=\"Code\">💻</a> <a href=\"#ideas-Andarist\" title=\"Ideas, Planning, & Feedback\">🤔</a> <a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=Andarist\" title=\"Documentation\">📖</a></td><td align=\"center\"><a href=\"https://github.com/macbem\"><img src=\"https://avatars1.githubusercontent.com/u/12464061?v=4\" width=\"100px;\" alt=\"Maciej Bembenista\"/><br /><sub><b>Maciej Bembenista</b></sub></a><br /><a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=macbem\" title=\"Code\">💻</a> <a href=\"#ideas-macbem\" title=\"Ideas, Planning, & Feedback\">🤔</a> <a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=macbem\" title=\"Documentation\">📖</a></td><td align=\"center\"><a href=\"https://github.com/MichaelTontchev\"><img src=\"https://avatars0.githubusercontent.com/u/12261336?v=4\" width=\"100px;\" alt=\"Michael Tontchev\"/><br /><sub><b>Michael Tontchev</b></sub></a><br /><a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=MichaelTontchev\" title=\"Code\">💻</a> <a href=\"#ideas-MichaelTontchev\" title=\"Ideas, Planning, & Feedback\">🤔</a> <a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=MichaelTontchev\" title=\"Documentation\">📖</a></td><td align=\"center\"><a href=\"http://ThomasdH.blogspot.com\"><img src=\"https://avatars0.githubusercontent.com/u/3889750?v=4\" width=\"100px;\" alt=\"Thomas den Hollander\"/><br /><sub><b>Thomas den Hollander</b></sub></a><br /><a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=ThomasdenH\" title=\"Code\">💻</a> <a href=\"#ideas-ThomasdenH\" title=\"Ideas, Planning, & Feedback\">🤔</a> <a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=ThomasdenH\" title=\"Documentation\">📖</a></td><td align=\"center\"><a href=\"https://twitter.com/esamatti\"><img src=\"https://avatars3.githubusercontent.com/u/225712?v=4\" width=\"100px;\" alt=\"Esa-Matti Suuronen\"/><br /><sub><b>Esa-Matti Suuronen</b></sub></a><br /><a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=epeli\" title=\"Code\">💻</a> <a href=\"#ideas-epeli\" title=\"Ideas, Planning, & Feedback\">🤔</a> <a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=epeli\" title=\"Documentation\">📖</a></td></tr><tr><td align=\"center\"><a href=\"https://github.com/IlyaSemenov\"><img src=\"https://avatars1.githubusercontent.com/u/128121?v=4\" width=\"100px;\" alt=\"Ilya Semenov\"/><br /><sub><b>Ilya Semenov</b></sub></a><br /><a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=IlyaSemenov\" title=\"Code\">💻</a> <a href=\"#ideas-IlyaSemenov\" title=\"Ideas, Planning, & Feedback\">🤔</a> <a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=IlyaSemenov\" title=\"Documentation\">📖</a></td><td align=\"center\"><a href=\"https://codechecks.io\"><img src=\"https://avatars2.githubusercontent.com/u/46399828?v=4\" width=\"100px;\" alt=\"Code Checks\"/><br /><sub><b>Code Checks</b></sub></a><br /><a href=\"#review-codechecks\" title=\"Reviewed Pull Requests\">👀</a></td><td align=\"center\"><a href=\"http://www.nomiclabs.io\"><img src=\"https://avatars1.githubusercontent.com/u/176499?v=4\" width=\"100px;\" alt=\"Patricio Palladino\"/><br /><sub><b>Patricio Palladino</b></sub></a><br /><a href=\"#ideas-alcuadrado\" title=\"Ideas, Planning, & Feedback\">🤔</a></td><td align=\"center\"><a href=\"http://twitter.com/quezak2\"><img src=\"https://avatars0.githubusercontent.com/u/666206?v=4\" width=\"100px;\" alt=\"Artur Kozak\"/><br /><sub><b>Artur Kozak</b></sub></a><br /><a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=quezak\" title=\"Code\">💻</a> <a href=\"#ideas-quezak\" title=\"Ideas, Planning, & Feedback\">🤔</a></td><td align=\"center\"><a href=\"https://github.com/lucifer1004\"><img src=\"https://avatars2.githubusercontent.com/u/13583761?v=4\" width=\"100px;\" alt=\"Zihua Wu\"/><br /><sub><b>Zihua Wu</b></sub></a><br /><a href=\"https://github.com/krzkaczor/ts-essentials/commits?author=lucifer1004\" title=\"Code\">💻</a> <a href=\"#ideas-lucifer1004\" title=\"Ideas, Planning, & Feedback\">🤔</a></td></tr></table>\n\n<!-- ALL-CONTRIBUTORS-LIST:END -->\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! [Read more](./CONTRIBUTING.md)\n","readmeFilename":"README.md","gitHead":"115b520acee6b1ec4d2278f477a7a2920503239a","bugs":{"url":"https://github.com/krzkaczor/ts-essentials/issues"},"homepage":"https://github.com/krzkaczor/ts-essentials#readme","_id":"ts-essentials@3.0.0-beta.0","_npmVersion":"6.4.1","_nodeVersion":"8.14.0","_npmUser":{"name":"krzkaczor","email":"krzkaczor@gmail.com"},"dist":{"integrity":"sha512-poMt5gqX5wMs/x0owcVycaeCfH1Qr3rJUg49j6IzyTRzMD2Q0WskUyMvSIiZcUInG9hv6a1al5WcCKP5RPSIYw==","shasum":"826b32b4bd17d263a81074da686b9694a7694bb4","tarball":"http://registry-npm.ywwl.com/ts-essentials/download/ts-essentials-3.0.0-beta.0.tgz","fileCount":11,"unpackedSize":22596,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdFI2DCRA9TVsSAnZWagAAfIAP/AryXgm2JPkMGMq/vXde\neclJ4OBbgU+vlnyOUAN3a4XO14jBkSHZ+p0FuRAOYvWuqOq/UuW97Vizg4u5\nh8GG3V90dxHESkQbX6X662ICJcq1vn36+bhGJcy498CWthfYh8/YwjG0ssjH\nYsYu9Nrnw29XhCCTvNlG2+tamIlnbGWYL0M5oGabXuujIO3iKzWkbi+GUgGP\nYdcRoZMBd/DPcuI/71KHeN6gM7Anfhjkcd6I62OLGv/5C88St1O6LzPPYUo8\nG2S5gWGnGb21wNcov9xpCh1VWwhv7aW8vYo7FgRpt9NIAZDPAXUVilsz2hCZ\n0lUIj52neNWGP7zeOWMhcLXY6hXXsNhKig9SF577yWVa/9wJqu4JimuONmeb\nQQtmVJwE6OJSbsSAvqO3lgcnKHUNbjPh58BroeD2YEskkuctGpUwmFKZifrZ\n3/bMd/ZHt2sf9UkTjUiQI4iUJUiol4mB3j+t/M82t0vEaQfUlYKIGRQXR9s1\nPj5XpHHX5QxoZVH4feDhEmE2pA9MECbBWimypbqthQA+ff7wtDIXZvtkMnKr\nTLJ2OkMOLVSR8Zj5kwSD2N1yg12u+FtRHyMf2ggLx7Vq9vsB0QJM3C49DTnW\n4ud5uANuYUfbe21WqpBH7Ug6VsEss2ynEbeLgsWFxkPI9SICjz67rp2CDOUU\nh/kv\r\n=OVzg\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDxOsUEqnCkd4ga78nimIVHO18Rd4qMNo61mVyDkt+0sAIhAKp7HLKiPBLGSVg3xQWV7rnAI5BKyyvz+98xM+jwQgZG"}],"size":6922,"noattachment":false,"key":"/ts-essentials/-/ts-essentials-3.0.0-beta.0.tgz"},"maintainers":[{"name":"krzkaczor","email":"krzkaczor@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ts-essentials_3.0.0-beta.0_1561628034914_0.4895489848038397"},"_hasShrinkwrap":false,"publish_time":1561628035105,"_cnpm_publish_time":1561628035105,"dist-tags":{"beta":"3.0.0-beta.0","latest":"10.2.1"}}