{"id":16014861,"url":"https://github.com/kitten/runruntypes","last_synced_at":"2025-11-10T10:08:57.740Z","repository":{"id":78159890,"uuid":"167633926","full_name":"kitten/runruntypes","owner":"kitten","description":"A poor man's runtime-only type checker","archived":false,"fork":false,"pushed_at":"2019-01-28T15:29:48.000Z","size":62,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T06:21:31.423Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/kitten.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-26T00:52:07.000Z","updated_at":"2025-02-06T07:01:56.000Z","dependencies_parsed_at":"2023-02-26T22:15:42.863Z","dependency_job_id":null,"html_url":"https://github.com/kitten/runruntypes","commit_stats":{"total_commits":2,"total_committers":2,"mean_commits":1.0,"dds":0.5,"last_synced_commit":"e6aadf037ed1e8bfc5450bde2a3b454be5aa02ce"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kitten%2Frunruntypes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kitten%2Frunruntypes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kitten%2Frunruntypes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kitten%2Frunruntypes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kitten","download_url":"https://codeload.github.com/kitten/runruntypes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243894547,"owners_count":20365049,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-10-08T15:05:21.398Z","updated_at":"2025-11-10T10:08:57.699Z","avatar_url":"https://github.com/kitten.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# \u003csup\u003eRunRun\u003c/sup\u003eTypes\n\n`RunRunTypes` is a simple runtime-only type checker. It's a fairly specific and simple library\nand it might be for you if:\n\n- You're trying to write a build-step-less app and need type checking in development\n- Your types are not simple and you prefer Flow-like definitions for them\n- You don't care about Flow's comment-only syntax much\n\nWell, if you belong to this small exclusive club, then **Welcome!**\n\n\u003e **Disclaimer:** This is frankly a horrible idea that's not meant for real world stuff, but\n\u003e it's been quite fun to write.\n\n## Getting Started\n\nYou can install `RunRunTypes` like any other npm package:\n\n```sh\nyarn add --dev runruntypes\n# or\nnpm install --save-dev runruntypes\n```\n\n\u003e **NOTE:** It's recommended to not ship this package in production as it pulls\n\u003e in `@babel/parser` as a dependency, which is really not that small. It's fairly\n\u003e simple to stub out this library's API though.\n\n## Usage\n\n`RunRunTypes` consists of two easy to use functions, `gen` and `type`.\n\n```js\nimport { gen, type } from 'runruntypes';\n```\n\nThe `gen` function is used to attach a type signature to your function. Using this signature\nyou can constrain the arguments' types and the function's return type. The `gen` function will\nwrap your function and return one that performs the checks when called. A signature is basically\na Flow type annotation:\n\n```js\nconst fn = gen`\n  (string, number, number) =\u003e [string, number]\n`((str, a, b) =\u003e [str, a + b])\n\nfn('test', 1, 2) // returns: ['test', 3]\nfn('test', 1) // error!\nfn(1, 1, 1) // error!\n```\n\nThe above example shows a simple definition and how arguments are checked.\nSimilarly the return types are checked when your function completes:\n\n```js\nconst fn = gen`\n  (string, number, number) =\u003e [string, number]\n`((str, a, b) =\u003e null)\n\nfn('test', 1, 2) // error!\n```\n\nThe second function, `type` is used to alias type signatures to a variable. It can be used to predefine\nsome types which can then be interpolated into your `gen` definition.\n\n```js\nimport { gen, type } from 'runruntypes';\n\nconst Errorish = type`\n  { message: ?string }\n`;\n\nconst isError = gen`(${Errorish} | void) =\u003e bool`(err =\u003e {\n  return err !== undefined \u0026\u0026 !!err.message;\n});\n```\n\nThis extracts an object definition type outside of the `gen` definition. For comparison, without a type alias\nyou'd simply write this definition inline, which can become tedious if it's being used and repeated quite often:\n\n```js\nimport { gen } from 'runruntypes';\n\nconst isError = gen`\n  ({ message: ?string } | void) =\u003e bool\n`(err =\u003e {\n  return err !== undefined \u0026\u0026 !!err.message;\n});\n```\n\n`RunRunTypes` uses the `@babel/parser` to parse any input to it. A large subset of Flow type annotations\nwill be parsed without a problem and will be correctly check. You can always expect this library\nto throw a `TypeError` when it either:\n\n- doesn't understand your type definition\n- or; has caught a type error\n\n[Feel free to read more about Flow type annotations here](https://flow.org/en/docs/types/)\n\n## API\n\n### `type`\n\nA [tagged template literal](https://www.styled-components.com/docs/advanced#tagged-template-literals) that accepts\na Flow type definition consisting of any of the supported types and syntax.\n\nThis will return a function that checks the first argument against the type definition and returns `true` if it\npasses the type checks and `false` if it doesn't.\n\n```js\ntype`string`('test') // true\ntype`{ x: 1 }`({ x: 2 }) // false\n```\n\n### `gen`\n\nA [tagged template literal](https://www.styled-components.com/docs/advanced#tagged-template-literals) that accepts\na Flow arrow function type definition consisting of any of the supported types and syntax.\n\nIt expects any number of arguments and a return type. Normal type definitions that are not function definitions\nwill cause it to throw an error.\n\nIt returns a factory function that wraps any function passed as the first argument in a new type checked and\nguarded function, i.e. it'll expect the argument to comply to the type definition that has been defined\nand will enforce it.\n\n```js\ngen`string =\u003e string`(x =\u003e 'Hello, ' + x)('Luke')\ngen`(number, 4) =\u003e string`((a, b) =\u003e '' + a + b)(2, 4)\n```\n\n### Supported Types\n\nCurrently the following types are supported:\n\n- primitive types (`bool`, `number`, `string`, `null`, `void`, `any`)\n- literal types (e.g. `true`, `false`, `2`, `\"hello\"`)\n- nullables (`?number`, i.e. `null`, `undefined`, `number`)\n- arrays (`number[]`)\n- tuples (`[number, number]`)\n- objects (`{ x: number }` without support for optionals nor methods)\n- unions (`number | string` meaning that both are allowed)\n- intersections (`{ x: 1 } \u0026 { y: 2 }` meaning that both must match)\n- generic constructors (e.g. `Date` or `Element` which will simply check the constructor's name)\n- \"any\" functions or objects (write `Object` to match any object, `Function` to match any function)\n\nYou can mix and combine the types as you'd expect in a real typed language.\nRemember that `gen` expects a function definition and `type` any of the above.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkitten%2Frunruntypes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkitten%2Frunruntypes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkitten%2Frunruntypes/lists"}