tsconfig.json: types file works when in 'files', but not in 'include'
Asked Answered
S

1

7

In my TypeScript project I'm extending express Request type to add own properties. I have a file src/types/index.d.ts that looks similar to this:

declare module 'express-serve-static-core' {
  interface Request {
    customFlag?: boolean;
  }
}

Part of my tsconfig.json looks like this:

"include": [
    "src/**/*.ts",
    "./server.ts"
],

When I'm runnning the project, it crashes on req.customFlag usage (says that property does not exist for type). If I modify my tsconfig.json as follows:

"include": [
    "src/**/*.ts",
    "./server.ts"
],
"files": [
    "src/types/index.d.ts"
],

it starts to work smoothly.

Why is that? My understanding of TS docs is that include and files are interpreted the same way, the only difference is that the first allows to specify patterns.

If I run tsc --listFiles --noEmit, the result contains src/types/index.d.ts for both configs. Yet for the first one it's not working.

Saddle answered 16/9, 2022 at 7:47 Comment(4)
Could you add your tsconfig.json here?Cory
Are you trying to augment the types in the @types/express-serve-static-core package? If so, you aren't doing that properly.Chainsmoke
I would guess that order is important and the second config loads src/types/index.d.ts first. If you really wanted to you could confirm that by setting a breakpoint src/types/index.d.ts and another in your file where req.customFlag is used, and checking which gets triggered first in each configuration.Volumeter
By any chance do you have skipLibCheck options set to true?Licha
H
-1

I think the expression src/**/*.ts is just not fitting to the two dots in the filename index.d.ts.

So you might rename the file to include only one dot or try to adjust the expression.
This is just an example, assuming that you might want to include all files with one or two dots in the filename, it might be utterly wrong: src/**/*?.?*.ts

The important page in the documentation is about include.

Happiness answered 17/8, 2023 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.