How to share code between TypeScript projects?

后端 未结 5 760
猫巷女王i
猫巷女王i 2020-12-12 19:11

Let\'s say I have two projects with following file structure

/my-projects/

  /project-a/
    lib.ts
    app.ts
    tsconfig.json

  /project-b/
    app.ts           


        
5条回答
  •  没有蜡笔的小新
    2020-12-12 19:27

    This can be achieved with use of 'paths' property of 'CompilerOptions' in tsconfig.json

    {
      "compilerOptions": {
        "paths": {
          "@otherProject/*": [
            "../otherProject/src/*"
          ]
        }
      },
    }
    

    Below is a screenshot of folder structure.

    Below is content of tsconfig.json which references other ts-project

    {
      "compilerOptions": {
        "baseUrl": "./",
        "outDir": "./tsc-out",
        "sourceMap": false,
        "declaration": false,
        "moduleResolution": "node",
        "module": "es6",
        "target": "es5",
        "typeRoots": [
          "node_modules/@types"
        ],
        "lib": [
          "es2017",
          "dom"
        ],
        "paths": {
          "@src/*": [ "src/*" ],
          "@qc/*": [
            "../Pti.Web/ClientApp/src/app/*"
          ]
        }
      },
      "exclude": [
        "node_modules",
        "dist",
        "tsc-out"
      ]
    }
    

    Below is import statement to reference exports from other project.

    import { IntegrationMessageState } from '@qc/shared/states/integration-message.state';
    

提交回复
热议问题