Skip to content

Help! My Types Are Missing! (tsc vs ts-node disparity) #866

@haf

Description

@haf

Using node v11.12.0 and ts-node v8.3.0, I get different behaviour between tsc running inside a langauge server in vscode 3.5.2 (node_modules has 3.5.3) for extended interfaces, when running with node -r ts-node/register.

The full command line is: TS_NODE_PROJECT=tsconfig.server.json node --inspect=9229 -r ts-node/register server/index.ts.

tsconfig.json has contents:

{
  "compilerOptions": {
    "allowJs": true,
    "downlevelIteration": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "noUnusedLocals": false,
    "noUnusedParameters": true,
    "preserveConstEnums": true,
    "removeComments": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "strictNullChecks": true,
    "target": "es2020",
    "typeRoots": [
      "./node_modules/@types",
      "./types"
    ]
  },
  "exclude": [
    "node_modules",
    "cypress",
    "dist",
    "next.config.js"
  ],
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ]
}

and tsconfig.server.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "commonjs",
    "outDir": "dist",
    "target": "es2017",
    "isolatedModules": false,
    "noEmit": false
  }
}

does not apply, but causes an error. Folder structure;

$ tree types
types
├── custom.d.ts
├── dom.d.ts
├── express
│   └── index.d.ts

Contents of index.d.ts:

declare module 'http' {
  export interface IncomingMessage {
    logger: import('../../lib/logary/logger').SpanLogger;
  }
}

Error:

[nodemon] starting `node -r ts-node/register server/index.ts`

site/node_modules/ts-node/src/index.ts:245
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
server/idp/login.ts:18:9 - error TS2339: Property 'logger' does not exist on type 'Request'.

18     req.logger.info("GET /accounts/login Missing `login_challenge` param")
           ~~~~~~

    at createTSError (site/node_modules/ts-node/src/index.ts:245:12)
    at reportTSError (site/node_modules/ts-node/src/index.ts:249:19)
    at getOutput (site/node_modules/ts-node/src/index.ts:362:34)
    at Object.compile (site/node_modules/ts-node/src/index.ts:395:32)
    at Module.m._compile (site/node_modules/ts-node/src/index.ts:473:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:810:10)
    at Object.require.extensions.(anonymous function) [as .ts] (site/node_modules/ts-node/src/index.ts:476:12)
    at Module.load (internal/modules/cjs/loader.js:666:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:606:12)
    at Function.Module._load (internal/modules/cjs/loader.js:598:3)

workaround that makes it work:

/// <reference types="../../types/express" />
/// <reference types="../../node_modules/@types/express" />

This means that ts-node when used via ts-node/register does not load the typescript definitions from the configured folders.

This issue has a long discussion showing approximately the same:

#782 (comment)

Yes, I have read the docs, both the handbook and the readme about types; hence the pun in the title of this issue. I think it's an issue since ts-node and the ts lang server diverge.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions