1 year ago

#325387

test-img

Pezholio

`ts-node` ignoring type declarations in Production

I have a NestJS project with a couple of scripts that I run via ts-node. I have a small type defintion file in my /src directory that looks like this:

declare global {
  interface Session {
    searchResultUrl?: string;
  }
  namespace Express {
    interface Request {
      session: Session;
    }
  }
}
export {};

I load it in via my tsconfig.json file like so:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "esModuleInterop": true,
    "typeRoots": ["./node_modules/@types", "*.d.ts"]
  },
  "include": ["src"]
}

This all works locally, but when I run my ts-node script in production I get the following error:

/srv/app/node_modules/ts-node/src/index.ts:820
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/professions/professions.controller.ts:29:43 - error TS2339: Property 'searchResultUrl' does not exist on type 'Session & Partial<SessionData>'.

29     (request: Request) => request.session.searchResultUrl,
                                             ~~~~~~~~~~~~~~~

    at createTSError (/srv/app/node_modules/ts-node/src/index.ts:820:12)
    at reportTSError (/srv/app/node_modules/ts-node/src/index.ts:824:19)
    at getOutput (/srv/app/node_modules/ts-node/src/index.ts:1014:36)
    at Object.compile (/srv/app/node_modules/ts-node/src/index.ts:1322:43)
    at Module.m._compile (/srv/app/node_modules/ts-node/src/index.ts:1454:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Object.require.extensions.<computed> [as .ts] (/srv/app/node_modules/ts-node/src/index.ts:1458:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19) {
  diagnosticCodes: [ 2339 ]
}

This works regardless of whether I include the path to my ts-config file or not:

npx ts-node ./src/console.ts opensearch:reseed:organisations
npx ts-node --project ./tsconfig.json ./src/console.ts opensearch:reseed:organisations

Can someone tell me where I'm going wrong?

typescript

nestjs

ts-node

0 Answers

Your Answer

Accepted video resources