{"id":44259005,"url":"https://github.com/h-sifat/handy-types","last_synced_at":"2026-02-10T16:40:35.541Z","repository":{"id":57260665,"uuid":"415060335","full_name":"h-sifat/handy-types","owner":"h-sifat","description":"A collection of small utility functions to check javascript variable types","archived":false,"fork":false,"pushed_at":"2023-12-15T03:57:31.000Z","size":579,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-29T04:57:28.964Z","etag":null,"topics":["assertion-library","typescript","utility-types","validation-library"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/h-sifat.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-08T16:42:06.000Z","updated_at":"2023-04-01T16:39:15.000Z","dependencies_parsed_at":"2022-08-25T02:11:13.782Z","dependency_job_id":null,"html_url":"https://github.com/h-sifat/handy-types","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/h-sifat/handy-types","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h-sifat%2Fhandy-types","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h-sifat%2Fhandy-types/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h-sifat%2Fhandy-types/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h-sifat%2Fhandy-types/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/h-sifat","download_url":"https://codeload.github.com/h-sifat/handy-types/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h-sifat%2Fhandy-types/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29307926,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-10T16:09:25.305Z","status":"ssl_error","status_checked_at":"2026-02-10T16:08:52.170Z","response_time":65,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assertion-library","typescript","utility-types","validation-library"],"created_at":"2026-02-10T16:40:32.727Z","updated_at":"2026-02-10T16:40:35.533Z","avatar_url":"https://github.com/h-sifat.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Handy-Types\n\n![Module Type](https://img.shields.io/badge/Module%20Type-UMD-brightgreen)\n![Npm Version](https://img.shields.io/npm/v/handy-types)\n![GitHub Tag](https://img.shields.io/github/v/tag/h-sifat/handy-types)\n![GitHub Issues](https://img.shields.io/github/issues/h-sifat/handy-types)\n\nA simple and lite weight validation library to reduce boilerplate in your\nJavaScript / TypeScript validation code.\n\nIt consists of many utility type predicate functions like `\"integer\"`,\n`\"positive_integer\"`, `\"non_empty_string\"`, `\"plain_object\"` and so on. Still\nnot impressed? How about `\"string[]\"` to represent a string array or\n`\"non_empty_string | non_empty_string[]\"` to represent a non empty string or an\narray of non empty string(s)? TypeScript type annotation is also supported. The\nlibrary is fully tested and has **100%** test coverage.\n\n## Importing\n\nThe library uses **UMD** module system so it's compatible with all JavaScript\nmodule systems.\n\n### JavaScript / TypeScript\n\n```js\n// es modules\nimport { is, assert, cache, handyTypes, typeNames } from \"handy-types\";\n\n// commonjs modules\nconst { is, assert, cache, handyTypes, typeNames } = require(\"handy-types\");\n```\n\n### In HTML with the `\u003cscript\u003e` tag\n\n```html\n\u003cscript src=\"https://unpkg.com/handy-types\"\u003e\u003c/script\u003e\n\u003c!-- it will be available as a global object by the name \"handy_types\" --\u003e\n```\n\n## Usages\n\nThis library exposes the following 5 entities\n\n1. **handyTypes:** An object containing all the type predicate functions. For\n   example, `handyTypes.positive_integer(2); // true`.\n\n1. **typeNames:** An object containing all the names of predicate functions of\n   `handyTypes`.\n   For example: `typeNames[\"positive_integer\"]; // \"Positive Integer\"`.\n   It may be used to generate meaningful error messages.\n\n1. **is:** a predicate function that uses all the functions in `handyTypes`\n   object to validate data. For example, `is(\"string\", \"a string\"); // true`. It\n   has a `cache` method with the same function signature as itself. The\n   `it.cache()` method can be used to cache parsed schemas to improve\n   performance.\n\n1. **assert:** a utility function similar to `is` but used for making\n   assertions. It also has a `cache` method similar to `is`.\n\n1. **cache:** An object used to manage schema caches of `is.cache()` and\n   `assert.cache()` functions.\n\n### Type schema syntax\n\nA type schema is a string passed into the `is` and `assert` function to\nrepresent a type. There are three types of type schema:\n\n1. **Basic:** Just a simple handy type name such as `\"string\"`,\n   `\"non_empty_array\"` etc.\n\n1. **Array:** If we add the `\"[]\"` suffix after any handy type name it\n   represents an array of that type. So `\"string[]\"` would represent and array\n   of string.\n\n1. **Union:** We can combine two or more type schema with a pipe `\"|\"` character\n   to represent an union. For example, `\"string | string[]\"` to represent a\n   string or an array of string(s).\n\n## Usages of `is()`\n\nThe `is` predicate function has the following signature.\n\n```ts\ninterface Is {\n  \u003cType\u003e(schema: string, value: unknown): value is Type;\n  cache\u003cType\u003e(schema: string, value: unknown): value is Type;\n}\n```\n\nThe reason it's a generic function with a type parameter named `Type` is to\nsupport typescript type annotation. For example:\n\n#### TypeScript Example\n\n```ts\nlet value: unknown;\n\nif (is\u003cstring | string[]\u003e(\"non_empty_string | non_empty_string[]\", value)) {\n  value; // let value: string | string[]\n}\n```\n\nIn the if block the type of `value` variable is `string | string[]`. We've to\npass the type of value (`string | string[]`) manually because it's not possible\nto process an union schema with TypeScript to determine it's actual type.\n\nBut if this seems a little bit of extra work to you then you can use the basic\ntype predicate functions directly from the `handyTypes` object . For\nexample:\n\n```ts\nlet value: unknown;\n\nif (handyTypes.integer(value)) {\n  value; // let value: number\n}\n```\n\nHere in the if block the type of `value` variable will be set to\n`number` automatically. But the downsides of this approach are:\n\n1. We can't use array or union type schemas\n2. `handyTypes.integer(value)` doesn't seem intuitive because most of the time\n   a predicate function starts with the word **is** as a convention.\n\n#### JavaScript Example\n\nFor JavaScript just remove the generic type argument.\n\n```js\nconst hobbies = \"programming\";\nif (is(\"non_empty_string | non_empty_string[]\", hobbies)) {\n  hobbies;\n  // so `hobbies` is either a non_empty_string or a\n  // non_empty_string array\n}\n```\n\n### Usages of `is.cache()`\n\nUse the `is.cache()` function instead of `is` for **array** and **union**\nschemas to improve performance. It will parse and cache the schema so that it\ndoesn't have to waste time parsing the same schema again and again.\n\n#### TypeScript Example\n\n```ts\nif (is.cache\u003cstring | string[]\u003e(\"string | string[]\", value)) {\n  value; // here value is of type: string | string[]\n}\n```\n\n#### JavaScript Example\n\n```ts\nif (is.cache(\"string | string[]\", value)) {\n  value; // here value is of type: string | string[]\n}\n```\n\n## Usages of `assert`\n\nWe can use the `assert` function to make assertions. It has the following\nfunction signature:\n\n```ts\ninterface Assert {\n  \u003cType\u003e(\n    schema: string,\n    value: unknown,\n    errorInfo?: ErrorInformation\n  ): asserts value is Type;\n  cache\u003cType\u003e(\n    schema: string,\n    value: unknown,\n    errorInfo?: ErrorInformation\n  ): asserts value is Type;\n}\n```\n\nHere `ErrorInformation` refers to the interface below.\n\n```ts\ninterface ErrorInformation {\n  name?: string;\n  message?: string;\n  code?: string | number;\n  otherInfo?: object;\n}\n```\n\nJust like the `is` function it takes a type schema and the variable we're making\nassertion on as it's first and second arguments respectively. Then we can\nprovide more information in the `errorInfo` object to customize the error\nobject.\n\nExamples:\n\n```ts\nlet value: unknown;\n\nassert\u003cnumber\u003e(\"integer\", value);\n// throws error: `Value must be of type Integer`\n\nassert\u003cnumber\u003e(\"integer\", value, {\n  name: \"Age\",\n  code: \"INVALID_AGE\",\n});\n// throws error: `Age must be of type Integer`, with code: \"INVALID_AGE\"\n\n// Use custom message instead of generating one\nassert\u003cstring\u003e(\"non_empty_string\", value, {\n  message: \"Invalid path\",\n  otherInfo: {\n    path: value,\n    errorId: -3,\n  },\n});\n// throws error: `Invalid path` , path: undefined, errorId: -3\n```\n\n### Usages of `assert.cache()`\n\nIt serves the same purpose as `is.cache()`, it caches parsed schemas.\n\n```ts\nassert.cache\u003cstring | string[]\u003e(\n  \"non_empty_string | non_empty_string[]\",\n  value,\n  { name: \"hobbies\" }\n); // use caching for improved performance\n```\n\n## Usages of the `cache` object\n\nThe `cache` object can be used to manage schema caches. It has the following\ninterface.\n\n```ts\nReadonly\u003c{\n  readonly size: number;\n  has(schema: string): boolean;\n  delete(schema: string): boolean;\n  clear(): void;\n}\u003e;\n```\n\nExamples:\n\n```ts\nconsole.log(cache.size); // 0\n\nconst schema = \"integer | integer[]\";\n\nis.cache\u003cnumber | number[]\u003e(schema, 23);\n\nconsole.log(cache.size); // 1\n\nconsole.log(cache.has(schema)); // true\n\n// use the delete method to delete a specific schema cache\ncache.delete(schema); // true\n\nconsole.log(cache.size); // 0\n\n// clear all caches\ncache.clear();\n```\n\n## All handy types\n\nBelow are the lists of all the type predicates available in the `handyTypes`\nobject.\n\n### Base Types\n\n| Type Name   | Full Name   | Implementation                                      |\n| ----------- | ----------- | --------------------------------------------------- |\n| boolean     | Boolean     | `typeof value === \"boolean\"`                        |\n| symbol      | Symbol      | `typeof value === \"symbol\"`                         |\n| string      | String      | `typeof value === \"string\"`                         |\n| object      | Object      | `typeof value === \"object\"`                         |\n| big_integer | Big Integer | `typeof value === \"bigint\"`                         |\n| function    | Function    | `typeof value === \"function\"`                       |\n| undefined   | Undefined   | `typeof value === \"undefined\"`                      |\n| **number**  | Number      | `typeof value === \"number\" \u0026\u0026 !Number.isNaN(value)` |\n\n**Note:** The type `number` is not just `typeof value === \"number\"`!\n\n### Number Types\n\n| Type Name           | Full Name           | Implementation                   |\n| ------------------- | ------------------- | -------------------------------- |\n| finite_number       | Finite Number       | `Number.isFinite(n)`             |\n| positive_number     | Positive Number     | `handyTypes.number(n) \u0026\u0026 n \u003e 0`  |\n| non_negative_number | Non Negative Number | `handyTypes.number(n) \u0026\u0026 n \u003e= 0` |\n| negative_number     | Negative Number     | `handyTypes.number(n) \u0026\u0026 n \u003c 0`  |\n| non_positive_number | Non Positive Number | `handyTypes.number(n) \u0026\u0026 n \u003c= 0` |\n\n### Integer Types\n\n| Type Name            | Full Name            | Implementation                  |\n| -------------------- | -------------------- | ------------------------------- |\n| integer              | Integer              | `Number.isInteger(i)`           |\n| safe_integer         | Safe Integer         | `Number.isSafeInteger(i)`       |\n| positive_integer     | Positive Integer     | `Number.isInteger(i) \u0026\u0026 i \u003e 0`  |\n| non_negative_integer | Non Negative Integer | `Number.isInteger(i) \u0026\u0026 i \u003e= 0` |\n| negative_integer     | Negative Integer     | `Number.isInteger(i) \u0026\u0026 i \u003c 0`  |\n| non_positive_integer | Non Positive Integer | `Number.isInteger(i) \u0026\u0026 i \u003c= 0` |\n\n### Bytewise Integer Types\n\n| Type Name              | Full Name               | Range                                   |\n| ---------------------- | ----------------------- | --------------------------------------- |\n| 8bit_integer           | 8 Bit Integer           | **-128** to **127**                     |\n| 8bit_unsigned_integer  | 8 Bit Unsigned Integer  | **0** to **255**                        |\n| 16bit_integer          | 16 Bit Integer          | **-32,768** to **32,767**               |\n| 16bit_unsigned_integer | 16 Bit Unsigned Integer | **0** to **65,535**                     |\n| 32bit_integer          | 32 Bit Integer          | **-2,147,483,648** to **2,147,483,647** |\n| 32bit_unsigned_integer | 32 Bit Unsigned Integer | **0** to **4,294,967,295**              |\n\n### Array Types\n\n| Type Name       | Full Name       | Implementation                               |\n| --------------- | --------------- | -------------------------------------------- |\n| array           | Array           | `Array.isArray(value)`                       |\n| non_empty_array | Non-Empty Array | `Array.isArray(value) \u0026\u0026 value.length !== 0` |\n\n### Object Types\n\n| Type Name       | Full Name       | Implementation                                                         |\n| --------------- | --------------- | ---------------------------------------------------------------------- |\n| non_null_object | Non-Null Object | `typeof value === \"object\" \u0026\u0026 value !== null`                          |\n| plain_object    | Plain Object    | `typeof value === \"object\" \u0026\u0026 value !== null \u0026\u0026 !Array.isArray(value)` |\n\n### String Types\n\n| Type Name                | Full Name                  | Implementation                                       |\n| ------------------------ | -------------------------- | ---------------------------------------------------- |\n| non_empty_string         | Non-Empty String           | `typeof value === \"string\" \u0026\u0026 value !== \"\"`          |\n| trimmed_non_empty_string | Non-Empty String (trimmed) | `typeof value === \"string\" \u0026\u0026 !!value.trim().length` |\n\n### Other Types\n\n| Type Name   | Full Name    | Implementation                            |\n| ----------- | ------------ | ----------------------------------------- |\n| nan         | Not A Number | `Number.isNaN(value)`                     |\n| any         | Any          | `true` // returns true for any value      |\n| nullish     | Nullish      | `value === null \\|\\| value === undefined` |\n| non_nullish | Non-Nullish  | `value !== null \u0026\u0026 value !== undefined`   |\n\nIf you find any bug or want to improve something please feel free to open an\nissue. Pull requests are also welcomed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh-sifat%2Fhandy-types","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fh-sifat%2Fhandy-types","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh-sifat%2Fhandy-types/lists"}