ts-transformer-keys: ts_transformer_keys_1.keys is not a function

Hi there!

This package sounds like the ideal solution to a problem I have but I am struggling to get past the following error: “TypeError: ts_transformer_keys_1.keys is not a function” and I am not sure how to tell if this is an error on my part or if there is something out of my control that is not working.

I have tried compiling the following file with typescript 2.4.0 and running in the browser with no other packages:

import { keys } from "ts-transformer-keys";

interface Props {
    id: string;
    name: string;
    age: number;
}
const keysOfProps = keys<Props>();

console.log(keysOfProps); 

I have also tried using ts-node and executing the same file from the command line, though here in particular I am unsure to what extend ts-node is a bottleneck.

I hope I’m missing something super simple!

Thanks in advance for any thoughts in any direction!

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 47
  • Comments: 50 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Was this fixed? I am getting the same issue

@martnst Sorry for the late response. If you are using Windows, the issue is probably fixed by this commit. Please try v0.3.1.

I get the same TypeError: ts_transformer_keys_1.keys is not a function by simply adding it via yarn add -D ts-transformer-keys and then trying to use it as of the example. It seems there is some step missing in the docs?

Hi @kimamula I got around the issue… was a problem with how i used the transformer. thanks for the library… helps to solve a major issue i was facing with

I have the same error TypeError: ts_transformer_keys_1.keys is not a function

I do not have webpack

I have only tsconfig.json and package.json

How can I fix this error?

I’m in the exact same scenario above. Any solution or workaround on this issue?

I had the same problem i had to follow it using ttypescript.

  1. npm install --save-dev typescript ttypescript
  2. in package.json on "start": use ttsc instead of tsc
{
  "name": "api",
  "version": "0.0.0",
  "scripts": {
    "start": "ttsc && node ./dist/server",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  }
  1. in tsconfig.json include plugin -> transform
{
  "compilerOptions": {
    ...
    "plugins": [
      { 
        "transform": "ts-transformer-keys/transformer"
      }
    ]
  },
  ...
}

I stopped getting the error after that 😅

How are you transforming keys()? As listed in README, there are typically 4 ways to transform keys().

I’m using TypeScript with React-Native, and error ts_transformer_keys_1.keys still appears, this is my devDendencies:

		"babel-jest": "^24.9.0",
		"babel-plugin-module-resolver": "^4.0.0",
		"eslint": "^6.5.1",
		"eslint-import-resolver-babel-module": "^5.1.2",
		"eslint-plugin-import": "^2.20.2",
		"eslint-plugin-module-resolver": "^0.16.0",
		"jest": "^24.9.0",
		"metro-react-native-babel-preset": "^0.58.0",
		"react-native-schemes-manager": "^2.0.0",
		"react-test-renderer": "16.11.0",
		"reactotron-react-native": "^5.0.0",
		"reactotron-redux": "^3.1.3",
		"reactotron-redux-saga": "^4.2.3",
		"ts-transformer-keys": "0.4.1",
		"tslint": "^6.1.2",
		"ttypescript": "^1.5.10",
		"typescript": "^3.8.3"

and tsconfig.json:

{
	"compilerOptions": {
		"allowJs": true,
		"allowSyntheticDefaultImports": true,
		"esModuleInterop": true,
		"isolatedModules": false,
		"jsx": "react",
		"lib": ["es6", "dom", "es2017"],
		"moduleResolution": "node",
		"noEmit": true,
		"strict": true,
		"target": "esnext",
		"baseUrl": ".",
		"paths": {
			"*": ["src"]
		},
		"plugins": [{ "transform": "ts-transformer-keys/transformer" }]
	},
	"exclude": [
		"node_modules",
		"babel.config.json",
		"metro.config.json",
		"jest.config.json"
	]
}

How can i fix it?

For me the issue was that I used the fork-ts-checker-webpack-plugin and therefore set transpileOnly: true. If transpileOnly is true, this plugin does not work. Makes kinda sense, took me still quite a while to figure it out 😉.

TLDR: ensure transpileOnly is not set to true!

Regards Simon

Using nodejs with ts-node as ts-node ./src/server.ts and getting TypeError: (0 , ts_transformer_keys_1.keys) is not a function

code

import { keys } from 'ts-transformer-keys';

const nestKeys = keys<Nest>();

I’m on a mac. it seems like it is working for me with ttypecript (vsc hinting shows me that I have an array of union of valid properties), but in ts-jest I am getting this error. I believe my setup is equivalent to what is provided in the readme… I have the same custom file and this in package.json (instead of jest.config):

  "jest": {
    "preset": "ts-jest",
    "testEnvironment": "node",
    "roots": [
      "<rootDir>/src"
    ],
    "moduleDirectories": [
      "node_modules",
      "src"
    ],
    "moduleNameMapper": {
      "@lib/(.*)": "<rootDir>/src/lib/$1"
    },
    "globals": {
      "ts-jest": {
        "astTransformers": { "before": ["./ts-jest-keys-transformer.js"] }
      }
    }

nvm, I realized that I just forgot to run jest --clearCache first … it started working when I changed the factory to:

const factory = (cs) => (ctx) => {
  console.log(keysTransformer)
  return keysTransformer(cs.tsCompiler.program)(ctx)
};

I have this issue as well with pnpm when while using ts-node --compiler ttypescript. No issues with ttsc. It seems that the issue here is the filepath mismatch due to symbolic link created by pnpm while using ts-node.

It can be fixed by changing

https://github.com/kimamula/ts-transformer-keys/blob/8a92a56719e97e1e849de5670148f09ff63aaf3f/transformer.ts#L61

to

import * as fs from 'fs';

...

 && (fs.realpathSync(path.join(declaration.getSourceFile().fileName)) === indexTs)

Not sure if this is the right way to fix it. But it works.

@llanginger were you able to come up with a workaround. cause I too ran into the same issue trying to use this package.

@kimamula yup that works

@vmirage I found that using require.resolve() instead of path.join() works and published the fixed version (v0.4.2). Please give it a try. Thanks again for your report!

Hi,

I have listed several ways of how to use custom transformers in README. Which way are you using?

@macbric @martnst I confirmed the following works both on macOS and Windows (with TypeScript@2.8.3).

  • index.ts
import { keys } from 'ts-transformer-keys';

interface Props {
  id: string;
  name: string;
  age: number;
}
const keysOfProps = keys<Props>();

console.log(keysOfProps);
  • compile.js
const ts = require('typescript');
const keysTransformer = require('ts-transformer-keys/transformer').default;

const program = ts.createProgram(['index.ts'], {
  strict: true,
  noEmitOnError: true,
  target: ts.ScriptTarget.ES5
});

const transformers = {
  before: [keysTransformer(program)],
  after: []
};
const { emitSkipped, diagnostics } = program.emit(undefined, undefined, undefined, false, transformers);

if (emitSkipped) {
  throw new Error(diagnostics.map(diagnostic => diagnostic.messageText).join('\n'));
}
  • compile and run
$ node compile # creates index.js
$ node index   # outputs "[ 'id', 'name', 'age' ]"

Could you provide your code so that I can investigate the problem?