Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

typescript - Namespace not used from referenced project

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": []
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...