Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module augmentation doesn't works in ts-node but do work with tsc #1571

Closed
RomainBillot opened this issue Dec 20, 2021 · 2 comments
Closed

Comments

@RomainBillot
Copy link

Hi,

I am trying to extend (overload) restify typing. In fact, I want a propery $authenticatedUser to be globally available in all requests.

I've made this typings, which looks to work in VsCode:

// in types/restify/index.d.ts
declare module 'restify' {
  interface Request {
    /**
     * @description The authenticated user, only available if the user has been authenticated before
     */
    $authenticatedUser?: UserInfo // UserInfo is a custom type
  }
}

And I have a piece of code that fails to compile with ts-node:

// in auth.middleware.ts
req.$authenticatedUser = decodedToken;

It says "property $authenticatedUser is not found in type Request."

The weird thing is that: tsc --noEmit works fine.

PS: I've tried to add ./src/types/ to typeRoot in compilerOptions, but it doesn't work too in ts-node.

Am I missing something obvious here?

Thanks in advance!

Specifications

  • ts-node v9.1.1
  • node v12.22.7
  • compiler v4.4.4
  • tsconfig.json, if you're using one:
{
  "extends": "./tsconfig.base.json",
  "include": ["./src/**/*.ts", "./test/**/*.ts"],
}

tsconfig.base.json:

{
  "compilerOptions": {
    "lib": ["es5", "es2015.promise", "dom"],
    "module": "commonjs",
    "outDir": "dist",
    "sourceMap": true,
    "target": "es6",
    "moduleResolution": "node",
    "noImplicitAny": false,
    "strictNullChecks": false,
    "typeRoots": ["./node_modules/@types"],
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "types": ["node", "jest"],
    "baseUrl": "./src",
  },
  "include": ["./src/**/*.ts"],
  "exclude": ["./node_modules"]
}
  • Operating system and version: MacOS latest
@blutorange
Copy link

This was discussed in #745 (comment)

The tsconfig.json is missing the custom types directory in the typeRoots. It's also important to put the custom directory before the builtin @types directory. Also make sure you actually augment the module by importing the Request interface from restify, otherwise it won't find the original properties of the Request.

For future reference, here's how to get the mentioned example work:

// types/restify/index.d.ts

// Make sure you import from the original restify module!
import { Request } from "restify";

declare module 'restify' {
    interface Request {
        $authenticatedUser?: { a: string };
    }
}

// src/index.ts
import { Request } from "restify";

function main(req: Request) {
    console.log(req.complete);
    console.log(req.$authenticatedUser);
}

main({ complete: true, $authenticatedUser: { a: "foo" } } as unknown as Request)

The package.json

{
  "dependencies": {
    "@types/restify": "^8.5.4",
    "restify": "^8.6.0",
    "ts-node": "^10.4.0",
    "typescript": "^4.5.4"
  }
}

and the tsconfig.json

{
  "include": [
    "src", "types"
  ],
  "compilerOptions": {
    "outDir": "dist",
    "target": "es5",
    "lib": [
      "es5"
    ],
    // Make sure types goes before @types !
    "typeRoots" : ["./types", "./node_modules/@types"]
  }
}

Then running npx ts-node src/index.ts will log

true
{ a: 'foo' }

@cspotcode
Copy link
Collaborator

Closing as answered. Friendly reminder that the issue tracker is reserved for bugs and feature requests. Support requests should be asked in the TypeScript Discord or the discussion forum: https://github.com/typeStrong/ts-node/discussions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants