{"id":20645955,"url":"https://github.com/jakecyr/derive-ts","last_synced_at":"2025-04-16T02:38:31.557Z","repository":{"id":47929868,"uuid":"393189303","full_name":"jakecyr/derive-ts","owner":"jakecyr","description":"Derive a TypeScript interface from a JavaScript object or JSON.","archived":false,"fork":false,"pushed_at":"2023-10-06T18:55:51.000Z","size":123,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T19:14:26.661Z","etag":null,"topics":["javascript","json","nodejs","typescript"],"latest_commit_sha":null,"homepage":"","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/jakecyr.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}},"created_at":"2021-08-05T22:59:00.000Z","updated_at":"2024-08-25T17:23:25.000Z","dependencies_parsed_at":"2022-08-12T14:30:56.673Z","dependency_job_id":null,"html_url":"https://github.com/jakecyr/derive-ts","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakecyr%2Fderive-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakecyr%2Fderive-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakecyr%2Fderive-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakecyr%2Fderive-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jakecyr","download_url":"https://codeload.github.com/jakecyr/derive-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249185151,"owners_count":21226522,"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":["javascript","json","nodejs","typescript"],"created_at":"2024-11-16T16:22:48.228Z","updated_at":"2025-04-16T02:38:31.524Z","avatar_url":"https://github.com/jakecyr.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Derive TypeScript Interface\n\nSimple functions to derive a TypeScript interface from a JavaScript object example.\n\n## Installation\n\n### CLI Installation\n\n```bash\nnpm install -g derive-ts\n# OR\nnpx derive-ts\n```\n\n### For Use in NodeJS\n\nThe deriveInterfaceFromObject function can be imported and used in code also. The deriveInterfaceFromObject function accepts three parameters: a JavaScript object to convert to a TypeScript interface, an interface name to use in the generated TypeScript, and optionally a boolean flag if the code should be prettified or not (defaults to true).\n\nInstall the package locally in your project:\n\n```bash\nnpm install --save derive-ts\n```\n\nand then:\n\n```javascript\nconst { deriveInterfaceFromObject } = require('derive-ts');\n\nderiveInterfaceFromObject({ a: 1, b: 2, c: 3 }, 'MyInterface', true)\n  .then((typeScriptInterfaceString) =\u003e {\n    console.log(typeScriptInterfaceString);\n  })\n  .catch((error) =\u003e {\n    console.error(error);\n  });\n```\n\nThe above will log the following:\n\n```typescript\nexport interface MyInterface {\n  a: number;\n  b: number;\n  c: number;\n}\n```\n\nThe prettifyCode code function is also exported from the library so you can pass in your own prettier options to format the generated TypeScript. It accepts two parameters: the code string to format and an object with the prettier options.\n\n## CLI Usage\n\nThe CLI can be used by installing the `derive-ts` package or with `npx`:\n\n```bash\nnpm install --global derive-ts\nderive-ts derive examples/example.js -n Test -i example -o output.ts\n\n# OR\n\nnpx derive-ts derive examples/example.js -n Test -i example -o output.ts\n```\n\n### derive\n\nThis is the main command that accepts parameters of a JavaScript file containing the example object to derive the TypeScript interface from.\n\n#### Arguments\n\n**-n, --interface-name \u0026lt;name\u0026gt;**\nName of the outputted interface\n\n**-i, --import-name \u0026lt;importName\u0026gt;**\nName of the object exported from the specified file\n\n**-o, --output-file \u0026lt;outputFilePath\u0026gt;**\nFile to save the interface to\n\n#### Example\n\n### Simple Example\n\nA JavaScript file has been created called 'example.js' with the following contents:\n\n```javascript\nexports.example = {\n  address_components: [\n    { long_name: '8035', short_name: '8035', types: ['street_number'] },\n    { long_name: 'Market Street', short_name: 'Market St', types: ['route'] },\n    { long_name: 'Wilmington', short_name: 'Wilmington', types: ['locality', 'political'] },\n  ],\n  adr_address:\n    '\u003cspan class=\"street-address\"\u003e8035 Market St\u003c/span\u003e, \u003cspan class=\"locality\"\u003eWilmington\u003c/span\u003e, \u003cspan class=\"region\"\u003eNC\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e28411\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eUSA\u003c/span\u003e',\n  business_status: 'OPERATIONAL',\n  formatted_address: '8035 Market St, Wilmington, NC 28411, USA',\n  formatted_phone_number: '(910) 686-2007',\n\n  vicinity: '8035 Market Street, Wilmington',\n  website:\n    'https://restaurants.subway.com/united-states/nc/wilmington/8035-market-st?utm_source=yxt-goog\u0026utm_medium=local\u0026utm_term=acq\u0026utm_content=60848\u0026utm_campaign=evergreen-2020\u0026y_source=1_MTQ4OTUyNzYtNzE1LWxvY2F0aW9uLmdvb2dsZV93ZWJzaXRlX292ZXJyaWRl',\n};\n```\n\nAfter running the following command:\n\n```bash\nderive-ts derive ./example.js --output-file output.ts --interface-name Test --import-name example\n```\n\nThe following file is generated 'output.ts' with the contents:\n\n```typescript\nexport interface Test {\n  address_components: {\n    long_name: string;\n    short_name: string;\n    types: string[];\n  }[];\n  adr_address: string;\n  business_status: string;\n  formatted_address: string;\n  formatted_phone_number: string;\n  vicinity: string;\n  website: string;\n}\n```\n\n### Example with Sub-Interfaces Generated\n\nA JavaScript file has been created called 'example.js' with the following contents:\n\n```javascript\nexports.example = {\n  address_components: [\n    { long_name: '8035', short_name: '8035', types: ['street_number'] },\n    { long_name: 'Market Street', short_name: 'Market St', types: ['route'] },\n    { long_name: 'Wilmington', short_name: 'Wilmington', types: ['locality', 'political'] },\n  ],\n  adr_address:\n    '\u003cspan class=\"street-address\"\u003e8035 Market St\u003c/span\u003e, \u003cspan class=\"locality\"\u003eWilmington\u003c/span\u003e, \u003cspan class=\"region\"\u003eNC\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e28411\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eUSA\u003c/span\u003e',\n  business_status: 'OPERATIONAL',\n  formatted_address: '8035 Market St, Wilmington, NC 28411, USA',\n  formatted_phone_number: '(910) 686-2007',\n\n  vicinity: '8035 Market Street, Wilmington',\n  website:\n    'https://restaurants.subway.com/united-states/nc/wilmington/8035-market-st?utm_source=yxt-goog\u0026utm_medium=local\u0026utm_term=acq\u0026utm_content=60848\u0026utm_campaign=evergreen-2020\u0026y_source=1_MTQ4OTUyNzYtNzE1LWxvY2F0aW9uLmdvb2dsZV93ZWJzaXRlX292ZXJyaWRl',\n};\n```\n\nAfter running the following command (notice the `--sub-interfaces` flag in the command)\n\n```bash\nderive-ts derive ./example.js --output-file output.ts --interface-name Test --import-name example --sub-interfaces\n```\n\nThe following file is generated 'output.ts' with the contents:\n\n```typescript\nexport interface AddressComponents {\n  long_name: string;\n  short_name: string;\n  types: string[];\n}\n\nexport interface Test {\n  address_components: AddressComponents[];\n  adr_address: string;\n  business_status: string;\n  formatted_address: string;\n  formatted_phone_number: string;\n  vicinity: string;\n  website: string;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjakecyr%2Fderive-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjakecyr%2Fderive-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjakecyr%2Fderive-ts/lists"}