{"id":17313374,"url":"https://github.com/janjakubnanista/ts-reflection","last_synced_at":"2025-04-14T14:20:31.519Z","repository":{"id":42795291,"uuid":"272534567","full_name":"janjakubnanista/ts-reflection","owner":"janjakubnanista","description":"Type inspection utilities for TypeScript","archived":false,"fork":false,"pushed_at":"2023-01-26T21:44:44.000Z","size":1972,"stargazers_count":31,"open_issues_count":35,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T03:14:48.555Z","etag":null,"topics":["enum","keys","properties","reflection","typescript","typescript-transformer"],"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/janjakubnanista.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}},"created_at":"2020-06-15T20:13:49.000Z","updated_at":"2024-11-19T16:37:35.000Z","dependencies_parsed_at":"2023-02-15T00:46:09.432Z","dependency_job_id":null,"html_url":"https://github.com/janjakubnanista/ts-reflection","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janjakubnanista%2Fts-reflection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janjakubnanista%2Fts-reflection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janjakubnanista%2Fts-reflection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janjakubnanista%2Fts-reflection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/janjakubnanista","download_url":"https://codeload.github.com/janjakubnanista/ts-reflection/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248894939,"owners_count":21179154,"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":["enum","keys","properties","reflection","typescript","typescript-transformer"],"created_at":"2024-10-15T12:46:44.500Z","updated_at":"2025-04-14T14:20:31.490Z","avatar_url":"https://github.com/janjakubnanista.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- Logo --\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg width=\"50%\" src=\"https://raw.githubusercontent.com/janjakubnanista/ts-reflection/main/res/ts-reflection.jpg\"/\u003e\n\u003c/p\u003e\n\n\u003ch1 align=\"center\"\u003e\n  ts-reflection\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  Type inspection utilities for TypeScript\n\u003c/p\u003e\n\n\u003c!-- The badges section --\u003e\n\u003cp align=\"center\"\u003e\n  \u003c!-- CircleCI build status --\u003e\n  \u003ca href=\"https://circleci.com/gh/janjakubnanista/ts-reflection/tree/main\"\u003e\u003cimg alt=\"CircleCI Build Status\" src=\"https://circleci.com/gh/janjakubnanista/ts-reflection.svg?style=shield\"\u003e\u003c/a\u003e\n  \u003c!-- Fury.io NPM published package version --\u003e\n  \u003ca href=\"https://www.npmjs.com/package/ts-reflection\"\u003e\u003cimg alt=\"NPM Version\" src=\"https://badge.fury.io/js/ts-reflection.svg\"/\u003e\u003c/a\u003e\n  \u003c!-- Shields.io dev dependencies status --\u003e\n  \u003ca href=\"https://github.com/janjakubnanista/ts-reflection/blob/main/package.json\"\u003e\u003cimg alt=\"Dev Dependency Status\" src=\"https://img.shields.io/david/dev/janjakubnanista/ts-reflection\"/\u003e\u003c/a\u003e\n  \u003c!-- Snyk.io vulnerabilities badge --\u003e\n  \u003ca href=\"https://snyk.io/test/github/janjakubnanista/ts-reflection\"\u003e\u003cimg alt=\"Known Vulnerabilities\" src=\"https://snyk.io/test/github/janjakubnanista/ts-reflection/badge.svg\"/\u003e\u003c/a\u003e\n  \u003c!-- Shields.io license badge --\u003e\n  \u003ca href=\"https://github.com/janjakubnanista/ts-reflection/blob/main/LICENSE\"\u003e\u003cimg alt=\"License\" src=\"https://img.shields.io/npm/l/ts-reflection\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ccode\u003ets-reflection\u003c/code\u003e allows you to access information about your types in runtime - e.g. get properties of a type or possible values of a union. It is compatible with \u003ca href=\"./docs/INSTALLATION.md#installation--rollup\"\u003erollup\u003c/a\u003e, \u003ca href=\"./docs/INSTALLATION.md#installation--webpack\"\u003ewebpack\u003c/a\u003e, and \u003ca href=\"./docs/INSTALLATION.md#installation--ttypescript\"\u003ettypescript\u003c/a\u003e projects and works nicely with \u003ca href=\"./docs/INSTALLATION.md#installation--jest\"\u003ejest\u003c/a\u003e or \u003ca href=\"./docs/INSTALLATION.md#installation--ts-node\"\u003ets-node\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"#motivation\"\u003eMotivation\u003c/a\u003e\n  \u003cspan\u003e|\u003c/span\u003e\n  \u003ca href=\"https://github.com/janjakubnanista/ts-reflection/blob/main/docs/API.md\"\u003eAPI\u003c/a\u003e\n  \u003cspan\u003e|\u003c/span\u003e\n  \u003ca href=\"https://github.com/janjakubnanista/ts-reflection/blob/main/docs/INSTALLATION.md\"\u003eInstallation\u003c/a\u003e\n  \u003cspan\u003e|\u003c/span\u003e\n  \u003ca href=\"#acknowledgement\"\u003eAcknowledgement\u003c/a\u003e\n\u003c/p\u003e\n\n## Wait what?\n\nAs they say *an example is worth a thousand API docs* so why not start with one.\n\n```typescript\nimport { propertiesOf } from 'ts-reflection';\n\ninterface MyInterface {\n  name: string;\n  hobbies: string[];\n}\n\n// You can now use propertiesOf utility to get properties of a type\nconst properties = propertiesOf\u003cMyInterface\u003e(); // ['name', 'hobbies']\n```\n\nLet's do another one!\n\n```typescript\nimport { valuesOf } from 'ts-reflection';\n\ntype ButtonType = 'primary' | 'secondary' | 'link';\n\n// You can use valuesOf utility to get all the possible union type values\nconst buttonTypes = valuesOf\u003cButtonType\u003e(); // ['primary', 'secondary', 'link']\n```\n\n\u003ca id=\"motivation\"\u003e\u003c/a\u003e\n## Motivation\n\nI can't count the number of times I needed to type all the possible values of a union type to create e.g. a dropdown with all the button types:\n\n```typescript\ntype ButtonType = 'primary' | 'secondary' | 'link';\n\nconst buttonTypes: ButtonType[] = ['primary', 'secondary', 'link'];\n```\n\nI was always aware of fragility of such solution and the fact you need to update it by hand every time `ButtonType` changes. Now I can write just\n\n```typescript\nconst buttonTypes: ButtonType[] = valuesOf\u003cButtonType\u003e;\n```\n\nThe same goes for a list of type properties - typing those lists of `keyof` type values:\n\n```typescript\ninterface MyInterface {\n  property: number;\n  anotherProperty: string;\n}\n\ntype Key = keyof MyInterface;\nconst keys: Key[] = ['property', 'anotherProperty']\n```\n\nWhich now becomes\n\n```typescript\nconst keys: Key[] = propertiesOf\u003cMyInterface\u003e();\n```\n\n## API\n\n_You can find comprehensive API documentation in the [API docs](https://github.com/janjakubnanista/ts-reflection/blob/main/docs/API.md)._\n\n`ts-reflection` exports two functions: `valuesOf` (for accessing values of union types) and `propertiesOf` (for accessing properties of types).\n\n### `valuesOf`\n\n`valuesOf` is a function that returns all the possible literal values of union types:\n\n```typescript\nimport { valuesOf } from 'ts-reflection';\n\ntype UnionType = 'string value' | 1 | true | Symbol.toStringTag;\n\n// You can use valuesOf utility to get all the possible union type values\nconst unionTypeValues = valuesOf\u003cUnionType\u003e(); // ['string value', 1, true, Symbol.toStringTag]\n```\n\nPlease read the full [API docs](https://github.com/janjakubnanista/ts-reflection/blob/main/docs/API_REFLECTION.md#valuesOf) for more information about `valuesOf`.\n\n### `propertiesOf`\n\n`propertiesOf` is a function that returns property names of types: \n\n```typescript\nimport { propertiesOf } from 'ts-reflection';\n\ninterface MyInterface {\n  name: string;\n  displayName?: string;\n  readonly hobbies: string[];\n}\n\n// When called with no arguments, propertiesOf() returns all public properties of a type\nconst properties = propertiesOf\u003cMyInterface\u003e(); // ['name', 'displayName', 'hobbies']\n\n// You can also call it with \"queries\" to be more specific about what properties you want to get\nconst readonlyProperties = propertiesOf\u003cMyInterface\u003e({ readonly: true }); // ['hobbies']\nconst mutableProperties = propertiesOf\u003cMyInterface\u003e({ readonly: false }); // ['name', 'displayName']\nconst optionalProperties = propertiesOf\u003cMyInterface\u003e({ optional: true }); // ['displayName']\nconst requiredProperties = propertiesOf\u003cMyInterface\u003e({ optional: false }); // ['name', 'hobbies']\n```\n\nPlease read the full [API docs](https://github.com/janjakubnanista/ts-reflection/blob/main/docs/API_REFLECTION.md#propertiesOf) for more information about `propertiesOf` and the queries it supports.\n\n## Installation\n\nYou can find comprehensive installation instructions in the [installation docs](https://github.com/janjakubnanista/ts-reflection/blob/main/docs/INSTALLATION.md).\n\n\u003ca id=\"acknowledgement\"\u003e\u003c/a\u003e\n## Acknowledgement\n\nThis idea was inspired by [`ts-transformer-keys`](https://www.npmjs.com/package/ts-transformer-keys) NPM module. The E2E testing infrastructure that ensures compatibility with all minor TypeScript versions is based on my [`ts-type-checked`](https://www.npmjs.com/package/ts-type-checked) project.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanjakubnanista%2Fts-reflection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjanjakubnanista%2Fts-reflection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanjakubnanista%2Fts-reflection/lists"}