Skip to content
Advertisement

Typescript’s declaration merging not working as expected using ts-node

For a project using the express-session package, I’m trying to mutate the session object by simply adding a user key.

JavaScript

Coming from this question’s accepted answer, I understand I could use declaration merging to extend the SessionData interface, using my own interface.

Looking at various open-source projects, such as the HospitalRun components repository I notice them having the types directory in their tsconfig.json file under the include section like this.

JavaScript

My whole tsconfig.json looks like this, which lives in the root of the project.

JavaScript

I tried doing the same, together a file called express-session.d.ts in the root of this folder (~/types/), having the following contents:

JavaScript

However, the error I keep receiving is this.

JavaScript

When I do however add this piece of code above the code I use for mutating my session object, I no longer have the problem. This doesn’t seem like the right approach though.

Also, when I use tsc src/index.ts --build instead of ts-node src/index.ts it also works.

What am I doing wrong here? How can this be fixed? I also tried using the typeRoots, using the same folder.

Advertisement

Answer

LATEST UPDATE (08-MAY-2021)

When running the typescript program by using ts-node, even typeRoots are specified in tsconfig.json, it cannot recognise the custom .d.ts and prompt Property 'x does not exist on type y` error.

According to https://github.com/TypeStrong/ts-node/issues/1132#issuecomment-716642560

One of the contributors of ts-node suggested multiple ways to solve it.

Here is one of it: Specifying file: true flag in tsconfig.json to inform ts-node to load files, include and exclude options from tsconfig.json on startup

JavaScript

OLD: (07-MAY-2021)

There is no need to use include in tsconfig.json and the paths are not correct. The compiler can search the ts file in the directory and sub-directories

Try to remove it. and restart TS server.

If you are using VSCode, try Cmd + Shift + P or Ctrl + Shift + P and search Restart TS server and see if the user type error still exist

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement