{"id":13433055,"url":"https://github.com/jvilk/MakeTypes","last_synced_at":"2025-03-17T10:33:06.256Z","repository":{"id":14472190,"uuid":"76596205","full_name":"jvilk/MakeTypes","owner":"jvilk","description":"Make TypeScript types and proxy objects from example JSON objects. Can use proxy objects to dynamically type check JSON at runtime.","archived":false,"fork":false,"pushed_at":"2023-03-08T02:05:22.000Z","size":1300,"stargazers_count":454,"open_issues_count":17,"forks_count":44,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-10-14T18:48:07.907Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jvilk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-12-15T21:07:53.000Z","updated_at":"2024-10-05T23:46:00.000Z","dependencies_parsed_at":"2024-01-16T01:26:14.919Z","dependency_job_id":null,"html_url":"https://github.com/jvilk/MakeTypes","commit_stats":{"total_commits":25,"total_committers":3,"mean_commits":8.333333333333334,"dds":0.12,"last_synced_commit":"9d57d7b03f9dded0556dfb067896b09c6b860891"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jvilk%2FMakeTypes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jvilk%2FMakeTypes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jvilk%2FMakeTypes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jvilk%2FMakeTypes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jvilk","download_url":"https://codeload.github.com/jvilk/MakeTypes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244016915,"owners_count":20384235,"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-07-31T02:01:20.399Z","updated_at":"2025-03-17T10:33:05.902Z","avatar_url":"https://github.com/jvilk.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# MakeTypes 1.1.2\n\u003e Generate TypeScript types and proxy classes from example JSON objects. Type check JSON objects at runtime!\n\n[![NPM version](https://badge.fury.io/js/maketypes.svg)](http://badge.fury.io/js/maketypes)\n[![Build Status](https://travis-ci.org/jvilk/MakeTypes.svg?branch=master)](https://travis-ci.org/jvilk/MakeTypes)\n[![Coverage Status](https://coveralls.io/repos/github/jvilk/MakeTypes/badge.svg?branch=master)](https://coveralls.io/github/jvilk/MakeTypes?branch=master)\n\nWith MakeTypes, you can interact with REST endpoints and other sources of JSON with confidence that\nyour program receives the data you expect. **All you need is a file containing representative sample JSON objects.**\n\n[Try the web demo](http://jvilk.com/MakeTypes/)!\n\nFeatures:\n\n* **Type-checked JSON.parse.** Proxy classes generated with MakeTypes will parse your JSON and check that it matches the expected type during runtime.\n* **Statically type-check code that interacts with JSON objects.** Proxy objects generated with MakeTypes are expressed as TypeScript classes, so you can statically type check that your code is appropriately accessing fields on the JSON object.\n* **Generate TypeScript interfaces to describe JSON types.** Don't want the overhead of runtime type checking, and trust that your samples are representative? You can opt to generate TypeScript interfaces instead, which describe the expected structure of the JSON object and facilitate type checking.\n\n\n## Install\n\n    npm i -g maketypes\n\n## Build\n\n    npm install\n\n## Usage\n\nFirst, write a file called `samples.json` that contains JSON samples for a particular object type, such as the value returned from a web service. It can either contain a single sample or an array of samples.\n\nThen, run:\n\n    make_types -i interfaces.ts -p proxies.ts samples.json RootName\n\n...where:\n\n* `interfaces.ts` will hold interface definitions for the JSON (optional)\n* `proxies.ts` will hold proxy class definitions that you can use to dynamically type check JSON objects at runtime (optional)\n* `RootName` specifies the name to use for the type that describes the JSON object\n\nMakeTypes will use simple heuristics to determine the names of nested sub-types.\n\n### Using Proxies\n\nMakeTypes generates proxy classes that dynamically check that runtime JSON objects match the expected static type.\nThey also standardize optional/nullable fields to contain `null` rather than `null` or `undefined`, which simplifies\nyour code.\n\nExample `samples.json`:\n\n```json\n[\n  {\n    \"foo\": \"lalala\"\n  },\n  {\n    \"foo\": \"hello\",\n    \"baz\": 32\n  }\n]\n```\n\nGeneration command:\n\n    make_types -p proxies.ts samples.json RootName\n\nProxy generated from example:\n\n```typescript\nexport class RootNameProxy {\n  public readonly foo: string;\n  public readonly baz: number | null;\n  public static Parse(d: string): RootNameProxy {\n    return RootNameProxy.Create(JSON.parse(d));\n  }\n  public static Create(d: any): RootNameProxy {\n    if (d === null || d === undefined) {\n      throwNull2NonNull(\"RootName\");\n    } else if (typeof(d) !== 'object') {\n      throwNotObject(\"RootName\");\n    } else if (Array.isArray(d)) {\n      throwIsArray(\"RootName\");\n    }\n    return new RootNameProxy(d);\n  }\n  private constructor(d: any) {\n    checkString(d.foo, false);\n    this.foo = d.foo;\n    checkNumber(d.baz, true);\n    if (d.baz === undefined) {\n      d.baz = null;\n    }\n    this.baz = d.baz;\n  }\n}\n```\n\nExample TypeScript code using proxy class:\n\n```typescript\nimport {RootNameProxy} from './proxies';\n\n// RootName.Create will throw an exception if rawJson does not match the type of RootName.\nconst proxyObject = RootNameProxy.Parse('{\"foo\": \"bar\"}');\n// Now, you can access all of the properties of the JSON object with confidence that they\n// actually exist.\nlet foo = proxyObject.foo; // TypeScript knows foo is a string\n// If one of the properties on the proxy is optional, then it will have a null value.\nlet baz = proxyObject.baz; // TypeScript knows baz is number | null. In this case, it will be null.\n```\n\n### Using Interfaces\n\nFor a lighterweighter option that provides no runtime guarantees, MakeTypes can also generate TypeScript interfaces that describe the expected structure of\nJSON objects. You can use these interfaces to typecheck code that interacts with JSON objects, but they cannot check if the JSON objects obey the static\ntype at runtime.\n\nInterfaces also succinctly express the static type that MakeTypes is inferring from your samples, so this feature can be a good debugging mechanism.\n\nExample `samples.json`:\n\n```json\n[\n  {\n    \"foo\": \"lalala\"\n  },\n  {\n    \"foo\": \"hello\",\n    \"baz\": 32\n  }\n]\n```\n\nGeneration command:\n\n    make_types -i interfaces.ts samples.json RootName\n\nInterface generated from example:\n\n```typescript\nexport interface RootName {\n  foo: string;\n  baz?: number | null;\n}\n```\n\nExample TypeScript code using interfaces:\n\n```typescript\nimport {RootName} from './interfaces';\n\nconst rawJson = \u003cRootName\u003e JSON.parse('{\"foo\": \"bar\"}');\nlet foo = rawJson.foo; // TypeScript knows foo i a string\nlet baz = rawJson.baz; // TypeScript knows that baz is an optional field that may not be there.\n```\n\n## Inspiration / Technique\n\nMakeTypes uses the Common Preferred Shape Relation from Petricek et al.'s PLDI 2016 paper, [\"Types from Data: Making Structured Data First-Class Citizens in F#\"](https://dl.acm.org/citation.cfm?id=2908115).\nSince JavaScript/TypeScript lacks a distinction between integer and floating point types, we merge the `float` and `int` types into a `number` type.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjvilk%2FMakeTypes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjvilk%2FMakeTypes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjvilk%2FMakeTypes/lists"}