I am using TypeScript for a microservice-oriented monorepo project with directory structure:
- root
- backend
- shared-files
-src...
- tsconfig.json
- microservice1
-src...
- tsconfig.json
- microservice2
-src...
- tsconfig.json
...
In order to enrich the Request interface I'm using the accepted answer from Extend Express Request object using Typescript, namely I create a file @types/express/index.d.ts
:
declare namespace Express {
export interface Request {
extraAttribute: string;
}
}
This solution works well for me, however I want this interface (along with other functionalities) to be available in different microservices.
I set up project reference using shared-files
as referenced. I can use types from shared-files
with no problems, however that declare namespace
/export interface
seem not to be taking effect, and I get Property 'extraAttribute' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'
on build.
tsconfig files for reference:
microservice1/tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "es5",
"module": "commonjs",
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"baseUrl": "./src",
"paths": {"@shared-files/*": ["../../shared-files/src/*"]},
"typeRoots": ["./node_modules/@types", "../shared-files/src/@types", "./src/@types"],
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"references": [
{ "path": "../shared-files"}
]
}
shared-files/tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"declarationMap": true,
"outDir": "./build",
"composite": true,
"strict": true,
"baseUrl": "./",
"typeRoots": ["./node_modules/@types", "./src/@types"],
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"references": []
}