{"id":13550620,"url":"https://github.com/kimamula/ts-transformer-keys","last_synced_at":"2025-06-19T06:39:33.425Z","repository":{"id":42493308,"uuid":"89049212","full_name":"kimamula/ts-transformer-keys","owner":"kimamula","description":"A TypeScript custom transformer which enables to obtain keys of given type","archived":false,"fork":false,"pushed_at":"2024-08-28T09:45:18.000Z","size":973,"stargazers_count":784,"open_issues_count":16,"forks_count":84,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-03T00:34:20.455Z","etag":null,"topics":[],"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/kimamula.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":"2017-04-22T06:23:22.000Z","updated_at":"2025-03-28T18:02:14.000Z","dependencies_parsed_at":"2023-11-26T06:24:38.025Z","dependency_job_id":"e0b83a95-7274-44e0-86f4-5ab1efabdb99","html_url":"https://github.com/kimamula/ts-transformer-keys","commit_stats":{"total_commits":99,"total_committers":9,"mean_commits":11.0,"dds":0.595959595959596,"last_synced_commit":"9d0cbe5e44977d61a3a04ad1c56732818f8915c1"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/kimamula/ts-transformer-keys","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimamula%2Fts-transformer-keys","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimamula%2Fts-transformer-keys/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimamula%2Fts-transformer-keys/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimamula%2Fts-transformer-keys/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kimamula","download_url":"https://codeload.github.com/kimamula/ts-transformer-keys/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimamula%2Fts-transformer-keys/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260703854,"owners_count":23049450,"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-08-01T12:01:35.400Z","updated_at":"2025-06-19T06:39:28.412Z","avatar_url":"https://github.com/kimamula.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","目录"],"sub_categories":["TypeScript"],"readme":"# ts-transformer-keys\nA TypeScript custom transformer which enables to obtain keys of given type.\n\n![Build Status](https://github.com/kimamula/ts-transformer-keys/workflows/test/badge.svg)\n[![NPM version][npm-image]][npm-url]\n[![Downloads](https://img.shields.io/npm/dm/ts-transformer-keys.svg)](https://www.npmjs.com/package/ts-transformer-keys)\n\n# Requirement\nTypeScript \u003e= 2.4.1\n\n# How to use this package\n\nThis package exports 2 functions.\nOne is `keys` which is used in TypeScript codes to obtain keys of given type, while the other is a TypeScript custom transformer which is used to compile the `keys` function correctly.\n\n## How to use `keys`\n\n```ts\nimport { keys } from 'ts-transformer-keys';\n\ninterface Props {\n  id: string;\n  name: string;\n  age: number;\n}\nconst keysOfProps = keys\u003cProps\u003e();\n\nconsole.log(keysOfProps); // ['id', 'name', 'age']\n```\n\n## How to use the custom transformer\n\nUnfortunately, TypeScript itself does not currently provide any easy way to use custom transformers (See https://github.com/Microsoft/TypeScript/issues/14419).\nThe followings are the example usage of the custom transformer.\n\n### webpack (with ts-loader or awesome-typescript-loader)\n\nSee [examples/webpack](examples/webpack) for detail.\n\n```js\n// webpack.config.js\nconst keysTransformer = require('ts-transformer-keys/transformer').default;\n\nmodule.exports = {\n  // ...\n  module: {\n    rules: [\n      {\n        test: /\\.ts$/,\n        loader: 'ts-loader', // or 'awesome-typescript-loader'\n        options: {\n          // make sure not to set `transpileOnly: true` here, otherwise it will not work\n          getCustomTransformers: program =\u003e ({\n              before: [\n                  keysTransformer(program)\n              ]\n          })\n        }\n      }\n    ]\n  }\n};\n\n```\n\n### Rollup (with rollup-plugin-typescript2)\n\nSee [examples/rollup](examples/rollup) for detail.\n\n```js\n// rollup.config.js\nimport resolve from 'rollup-plugin-node-resolve';\nimport typescript from 'rollup-plugin-typescript2';\nimport keysTransformer from 'ts-transformer-keys/transformer';\n\nexport default {\n  // ...\n  plugins: [\n    resolve(),\n    typescript({ transformers: [service =\u003e ({\n      before: [ keysTransformer(service.getProgram()) ],\n      after: []\n    })] })\n  ]\n};\n```\n\n### ttypescript\n\nSee [examples/ttypescript](examples/ttypescript) for detail.\nSee [ttypescript's README](https://github.com/cevek/ttypescript/blob/master/README.md) for how to use this with module bundlers such as webpack or Rollup.\n\n```jsonc\n// tsconfig.json\n{\n  \"compilerOptions\": {\n    // ...\n    \"plugins\": [\n      { \"transform\": \"ts-transformer-keys/transformer\" }\n    ]\n  },\n  // ...\n}\n```\n\n### ts-jest\n\nSee [examples/ts-jest](examples/ts-jest) for details.\nIn order to use this transformer with ts-jest, you need to add a wrapper around it like this:\n\n```javascript\n// ts-jest-keys-transformer.js\nconst keysTransformer = require('ts-transformer-keys/transformer').default;\nconst name = 'my-key-transformer';\nconst version = 1;\nconst factory = (cs) =\u003e (ctx) =\u003e keysTransformer(cs.program)(ctx);\n// For ts-jest 26 use:\n// const factory = (cs) =\u003e (ctx) =\u003e keysTransformer(cs.tsCompiler.program)(ctx);\n\nmodule.exports = { name, version, factory };\n```\n\nAnd add it in `jest.config.js` like this:\n\n```javascript\n  globals: {\n    'ts-jest': {\n      // relative path to the ts-jest-keys-transformer.js file\n      astTransformers: { before: ['src/react/ts-jest-keys-transformer.js'] },\n    },\n  },\n```\n\nNote: ts-jest 26.4.2 does not work with this transformer (fixed in ts-jest 26.4.3). Also, for versions smaller than 26.2, you need to provide the transformer in an array instead, like this: `astTransformers: { before: ['src/react/ts-jest-keys-transformer.js'] }`\n\n### TypeScript API\n\nSee [test](test) for detail.\nYou can try it with `$ npm test`.\n\n```js\nconst ts = require('typescript');\nconst keysTransformer = require('ts-transformer-keys/transformer').default;\n\nconst program = ts.createProgram([/* your files to compile */], {\n  strict: true,\n  noEmitOnError: true,\n  target: ts.ScriptTarget.ES5\n});\n\nconst transformers = {\n  before: [keysTransformer(program)],\n  after: []\n};\nconst { emitSkipped, diagnostics } = program.emit(undefined, undefined, undefined, false, transformers);\n\nif (emitSkipped) {\n  throw new Error(diagnostics.map(diagnostic =\u003e diagnostic.messageText).join('\\n'));\n}\n```\n\nAs a result, the TypeScript code shown [here](#how-to-use-keys) is compiled into the following JavaScript.\n\n```js\n\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nvar ts_transformer_keys_1 = require(\"ts-transformer-keys\");\nvar keysOfProps = [\"id\", \"name\", \"age\"];\nconsole.log(keysOfProps); // ['id', 'name', 'age']\n```\n\n# Note\n\n* The `keys` function can only be used as a call expression. Writing something like `keys.toString()` results in a runtime error.\n* `keys` does not work with a dynamic type parameter, i.e., `keys\u003cT\u003e()` in the following code is converted to an empty array(`[]`).\n\n```ts\nclass MyClass\u003cT extends object\u003e {\n  keys() {\n    return keys\u003cT\u003e();\n  }\n}\n```\n\n# License\n\nMIT\n\n[npm-image]:https://img.shields.io/npm/v/ts-transformer-keys.svg?style=flat\n[npm-url]:https://npmjs.org/package/ts-transformer-keys\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkimamula%2Fts-transformer-keys","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkimamula%2Fts-transformer-keys","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkimamula%2Fts-transformer-keys/lists"}