{"id":17817082,"url":"https://github.com/gmazovec/flow-typer","last_synced_at":"2025-03-18T03:31:16.372Z","repository":{"id":57113629,"uuid":"122687295","full_name":"gmazovec/flow-typer","owner":"gmazovec","description":"Declarative static and runtime type checking with Flow","archived":false,"fork":false,"pushed_at":"2024-07-29T12:52:43.000Z","size":825,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T06:32:08.924Z","etag":null,"topics":["flow","flowtype","javascript","nodejs","runtime-typechecking","validation"],"latest_commit_sha":null,"homepage":"","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/gmazovec.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2018-02-24T00:33:44.000Z","updated_at":"2024-07-29T12:52:30.000Z","dependencies_parsed_at":"2024-10-27T17:26:08.157Z","dependency_job_id":null,"html_url":"https://github.com/gmazovec/flow-typer","commit_stats":{"total_commits":114,"total_committers":1,"mean_commits":114.0,"dds":0.0,"last_synced_commit":"e52c72c25645da77f582c6eb4229d72c2c979178"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazovec%2Fflow-typer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazovec%2Fflow-typer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazovec%2Fflow-typer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazovec%2Fflow-typer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gmazovec","download_url":"https://codeload.github.com/gmazovec/flow-typer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243896652,"owners_count":20365408,"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":["flow","flowtype","javascript","nodejs","runtime-typechecking","validation"],"created_at":"2024-10-27T16:40:25.478Z","updated_at":"2025-03-18T03:31:16.040Z","avatar_url":"https://github.com/gmazovec.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# flow-typer\n\n\u003e Declarative static and runtime type checking with Flow.\n\n[![npm](https://img.shields.io/npm/v/flow-typer-js.svg?colorB=brightgreen)](https://www.npmjs.com/package/flow-typer-js)\n[![Build Status](https://travis-ci.org/gmazovec/flow-typer.svg?branch=master)](https://travis-ci.org/gmazovec/flow-typer)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/0a7f801f54a49ffd63c7/test_coverage)](https://codeclimate.com/github/gmazovec/flow-typer/test_coverage)\n\nSo you are using [Flow](https://flow.org) to type check your code. That's great but how do you\ncheck types for data that is not known before running the code? Like _JSON_ input.\nSure, you can use your favorite validation library and do unsafe type casting. Or\nyou write verbose code and do low-level type checking with _typeof_ operator to\nsatisfy _Flow_'s refinement.\n\n_flow-typer_ is solving these problems by writing maintainable type schemas in\n_JavaScript_ with _Flow_ interoperability.\n\n![Flow Typer](./flow-typer.png)\n\n### Features\n\n- support for primitive and complex Flow types\n- complete _Flow_ coverage\n- type functions are immutable\n- define _Flow_ types with JavaScript\n- no transpilation required\n- works with ES6 JavaScript (modern browsers and Node 6+)\n\n\n## Installation\n\n```shell\nnpm install --save flow-typer-js\n```\n\n\n## Importing\n\n```js\nimport typer from 'flow-typer-js' // ES6\nvar typer = require('flow-typer-js') // ES5 with npm\n```\n\n\n## Usage\n\n_flow-typer_ exposes a set of functions for type checking at runtime. These\nfunctions are constructed in way that allows _Flow_ to infer types and keep\nrefinement of the code. By composing functions, we define a type schema that\ncan be used to create inferred _Flow_ types (static checking) and for validating\nvalues with unknown type at runtime.\n\n```js\nimport {\n  typeOf,\n  objectOf,\n  arrayOf,\n  tupleOf,\n  unionOf,\n  literalOf,\n  string,\n  number,\n  boolean,\n  maybe\n} from 'flow-typer-js'\n\nimport type { $Literal } from 'flow-typer-js'\n```\n\n```js\n// literal types require Flow annotation\nconst male$Literal = (literalOf('male'): $Literal\u003c'male'\u003e)\nconst female$Literal = (literalOf('female'): $Literal\u003c'female'\u003e)\n```\n\n```js\n// define type schema\nconst personSchema = objectOf({\n  name: string,\n  age: maybe(number),\n  active: boolean,\n  gender: unionOf(male$Literal, female$Literal),\n  tags: arrayOf(string),\n  location: tupleOf(number, number)\n})\n```\n\n```js\n// define Flow type from JS type schema\ntype PersonT = $Call\u003ctypeof personSchema\u003e\n```\n\n```js\n// check value of unknown type against type schema\nconst person = personSchema(unknownInput)\n// =\u003e person: PersonT\n```\n\n```js\n// type schema returns value of specific type\nperson.name.toUpperCase() // No error\nperson.email // Flow error (unknown attribute)\nperson.active = 1 // Flow error (boolean value expected)\n\n```\n\n## Errors\n\nType validation throws `TypeValidatorError` which contains useful information\nabout why validation failed and what kind of type is expected.\n\n```\nTypeValidatorError: invalid \"string\" value type; \"array\" type expected\n    ...\n\n    scope    PackageT.dependencies\n    expected Array\u003c{\"name\":\"string\",\"version\":\"string\"}\u003e\n    type     string\n    value    \"flow-typer\"\n    file     .../flow-typer-examples/index.js:15:15\n\n```\n\n- _scope_ - level at which validation failed\n- _expected_ - the expected type of input value\n- _type_ - the actual type of input value\n- _value_ - input value in JSON format\n- _file_ - file with position where the validator was called\n\n```js\ntype TypeValidatorError {\n  expectedType: string\n  valueType: string\n  value: string\n  typeScope: string\n  sourceFile: string\n}\n```\n\n## API\n\nThese functions will check for specific JavaScript type with correct _Flow_ type\nrefinement.\n\n- `typer.isNil`\n- `typer.isUndef`\n- `typer.isBoolean`\n- `typer.isNumber`\n- `typer.isString`\n- `typer.isObject`\n- `typer.isFunction`\n\n### Primitive types\n\n- `typer.nil`\n- `typer.undef`\n- `typer.boolean`\n- `typer.number`\n- `typer.string`\n- `typer.literalOf(value)` (requires _Flow_ annotations \\*)\n\n```js\nconst flow$Literal = (literalOf('flow'): $Literal\u003c'flow'\u003e) // =\u003e type T = 'flow'\n```\n\n### Complex types\n\n- `typer.mixed`\n- `typer.object`\n- `typer.maybe(schema)`\n- `typer.objectOf(schemaObject, label)`\n- `typer.optional(schema)`\n\n```js\nconst schema = objectOf({\n  username: string,\n  nickname: optional(string)\n})\n// =\u003e type T = {| username: string, nickname: (string | void) |}\n```\n\n- `typer.arrayOf(schema, label)`\n\n```js\nconst schema = arrayOf(number) // =\u003e type T = number[]\n```\n\n- `typer.tupleOf(...schema[])`\n\n```js\nconst schema = tupleOf(string, number) // =\u003e type T = [string, number]\n```\n\n- `typer.unionOf(...schema[])`\n\n```js\nconst schema = unionOf('week', 'month') // =\u003e type T = 'week' | 'month'\n```\n\n- `typer.mapOf(keySchema, valueSchema)`\n\n```js\nconst schema = mapOf(string, boolean) // =\u003e type T = { [_string]: boolean }\n```\n\n\n### Utilities\n\n- `typer.isType(schema): boolean`\n- `typer.getType(schema): string`\n\n```js\nconst schema = objectOf({\n  dependencies: arrayOf(objectOf(\n    name: string,\n    version: number,\n    exact: boolean\n  ))\n})\n\ngetType(schema)\n// =\u003e {| dependencies: Array\u003c{| name: string, version: number, exact: boolean |}\u003e |}\n```\n\n\n## TODO\n\n- Use `literalOf` without explicit _Flow_ type annotations. Literal type\ncan not be inferred by _Flow_. This could be solved with new Flow utility\ntypes `$Literal`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmazovec%2Fflow-typer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgmazovec%2Fflow-typer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmazovec%2Fflow-typer/lists"}