2

I'm trying to use ts-transformation-keys in my Nest application.

I started a new application using a template from this repository, and my machine is running with windows 10.

As described in the ts-transformation-keys documentation, i added the following configuration in the tsconfig.json file:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": false,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "plugins": [
      { "transform": "ts-transformer-keys/transformer" },
    ]
  },
  "exclude": [
    "node_modules",
    "dist",
    "graphql",
    "onboard"
  ]
}

That's my package.json file:

{
  "name": "nestjs-prisma-client-starter",
  "version": "0.0.1",
  "description": "NestJS Prisma Client Starter Project",
  "author": "Marc Stammerjohann",
  "license": "MIT",
  "keywords": [
    "NestJS",
    "Prisma",
    "Prisma Client",
    "Typescript"
  ],
  "repository": {
    "type": "git",
    "url": "https://github.com/fivethree-team/nestjs-prisma-client-example.git"
  },
  "bugs": {
    "url": "https://github.com/fivethree-team/nestjs-prisma-client-example/issues"
  },
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "migrate:dev": "prisma migrate dev --preview-feature",
    "migrate:dev:create": "prisma migrate dev --create-only --preview-feature",
    "migrate:reset": "prisma migrate reset --preview-feature",
    "migrate:deploy": "npx prisma migrate deploy --preview-feature",
    "migrate:status": "npx prisma migrate status --preview-feature",
    "migrate:resolve": "npx prisma migrate resolve --preview-feature",
    "prisma:studio": "npx prisma studio",
    "prisma:generate": "npx prisma generate",
    "prisma:generate:watch": "npx prisma generate --watch",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "start:db": "npm run migrate:up && npm run prisma:generate && npm run seed",
    "seed": "ts-node prisma/seed.ts",
    "postinstall": "npm run prisma:generate",
    "docker:migrate": "docker-compose -f docker-compose.migrate.yml up -d",
    "docker:db": "docker-compose -f docker-compose.db.yml up -d",
    "docker:seed": "docker exec -it nest-api npm run seed",
    "docker": "docker-compose up -d",
    "docker:build": "docker-compose build"
  },
  "dependencies": {
    "@devoxa/prisma-relay-cursor-connection": "1.1.1",
    "@nestjs/common": "7.6.15",
    "@nestjs/config": "0.6.3",
    "@nestjs/core": "7.6.15",
    "@nestjs/graphql": "7.10.3",
    "@nestjs/jwt": "7.2.0",
    "@nestjs/passport": "7.1.5",
    "@nestjs/platform-express": "7.6.15",
    "@nestjs/swagger": "4.8.0",
    "@prisma/client": "2.20.1",
    "apollo-server-express": "2.22.2",
    "bcrypt": "5.0.1",
    "class-transformer": "0.4.0",
    "class-validator": "0.13.1",
    "dayjs": "^1.10.4",
    "graphql": "15.5.0",
    "graphql-tools": "7.0.4",
    "linq-es2015": "^2.5.1",
    "parcel-plugin-ttypescript": "^1.0.2",
    "passport": "0.4.1",
    "passport-jwt": "4.0.0",
    "reflect-metadata": "0.1.13",
    "rxjs": "6.6.7",
    "swagger-ui-express": "4.1.6"
  },
  "devDependencies": {
    "@apollo/gateway": "0.26.0",
    "@nestjs/cli": "7.6.0",
    "@nestjs/testing": "7.6.15",
    "@types/bcrypt": "3.0.0",
    "@types/chance": "1.1.1",
    "@types/express": "4.17.11",
    "@types/jest": "26.0.22",
    "@types/node": "14.14.37",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "4.21.0",
    "@typescript-eslint/parser": "4.21.0",
    "chance": "1.1.7",
    "eslint": "7.23.0",
    "eslint-config-prettier": "8.1.0",
    "eslint-plugin-prettier": "3.3.1",
    "jest": "26.6.3",
    "prettier": "2.2.1",
    "prisma": "2.20.1",
    "prisma-dbml-generator": "0.5.0",
    "rimraf": "3.0.2",
    "supertest": "6.1.3",
    "ts-jest": "26.5.4",
    "ts-loader": "8.1.0",
    "ts-node": "9.1.1",
    "ts-transformer-keys": "^0.4.3",
    "tsconfig-paths": "3.9.0",
    "ttypescript": "^1.5.12",
    "typescript": "^4.2.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

So, here's the problem:

i already ran these scripts:

npm i -g @nestjs/cli
npm install
npm build
npm start:debug

All these things worked well... But when i'm trying to use the ts-transformation-keys it doesn't work.

I added this small piece of code in the src/app.module.ts:

class myTestClass
{
  id : string;
  name : string;
}

let test = keys<myTestClass>()

Then i got this error:

TypeError: ts_transformer_keys_1.keys is not a function
    at Object.<anonymous> (C:\Repos\EDM\backend\dist\app.module.js:26:34)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (C:\Repos\EDM\backend\dist\main.js:7:22)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)

It's generating this javascript code:

[...]
const ts_transformer_keys_1 = require("ts-transformer-keys");
class myTestClass {
}
let test = ts_transformer_keys_1.keys();
[...]

Does anyone know what is wrong in my configuration?

I already tried to use different versions of ts-transformation-keys, but looks like Nest is not loading the custom transform described in tsconfig.json file.

Sai Gummaluri
  • 1,340
  • 9
  • 16
Bruno Warmling
  • 362
  • 4
  • 14
  • Nest just calls tsc when compiling, it would likely require a first step before tsc to be able to compile it: "Unfortunately, custom transformers are currently not so easy to use. You have to use them with the TypeScript transformation API instead of executing tsc command." https://stackoverflow.com/a/43922291/835917 – Drew Wiens Aug 03 '21 at 20:08

0 Answers0