ts-interface-keys-transformer
ts-interface-keys-transformer
is inspired by ts-transformer-keys.
It uses a custom transformer to parse the keys in 'interface' and 'type' in compile stage in TypeScript
which support nested keys and optionality.
Usage
$ npm i -D typescript ttypescript ts-interface-keys-transformer
and then add following to "compilerOptions" field in tsconfig.json:
"plugins":
Use following command to run (assume index.js is the compiled file of index.ts):
$ ttsc -p tsconfig.json && node index.js
Works With Simple Interface
; // [ { name: 'a', modifiers: [], optional: false, type: 'string' },// { name: 'b', modifiers: [ 'readonly' ], optional: false, type: 'number' },// { name: 'c', modifiers: [], optional: true, type: 'boolean' } ]console.logkeys;
Works With Complex Interface
; // [ { "name": "a", "modifiers": [], "optional": false, "type": "string" },// { "name": "b", "modifiers": [], "optional": false, "type": "object" },// { "name": "b.b1", "modifiers": [], "optional": false, "type": "string" },// { "name": "b.b2", "modifiers": [], "optional": false, "type": "number" },// { "name": "c", "modifiers": [], "optional": false, "type": "array", "elementType": "string" },// { // "name": "d",// "modifiers": [],// "optional": false,// "type": "Array",// "elementKeys": [// {// "name": "d1",// "modifiers": [],// "optional": false,// "type": "string"// },// {// "name": "d2",// "modifiers": [],// "optional": false,// "type": "number"// }// ]// } ]console.logkeys;
Works With Intersection Type
; // [ { "name": "a", "modifiers": [], "optional": false, "type": "string" },// { "name": "b", "modifiers": [], "optional": false, "type": "string" } ]console.logkeys;
Works With Union Type
; // [ { "name": "a", "modifiers": [], "optional": false, "type": "string"} ]console.logkeys;
See more test cases in Tests
Build
npm run build
Run Tests
npm test