Skip to content

Instantly share code, notes, and snippets.

@tjunghans
Last active January 22, 2020 07:38
Show Gist options
  • Save tjunghans/00c5f31b1fcc1a3f5f7d38ab309ff675 to your computer and use it in GitHub Desktop.
Save tjunghans/00c5f31b1fcc1a3f5f7d38ab309ff675 to your computer and use it in GitHub Desktop.
Typescript References in Mono Repo

Typescript References in Mono Repo

Mono repo directory structure:

tsconfig.json
tsconfig-base.json
package.json
packages/node_modules/packageA/tsconfig.json
packages/node_modules/packageB/tsconfig.json
packages/node_modules/packageC/tsconfig.json

packageB is a dependency of packageA and packageC. Both packageA and packageC import functions, types and interfaces from packageB.

The root tsconfig.json has references to all packages in alphabetical order:

{
  "files": [],
  "references": [
    {
      "path": "./packages/node_modules/packageA"
    },
    {
      "path": "./packages/node_modules/packageB"
    },
    {
      "path": "./packages/node_modules/packageC"
    }
  ]
}

The tsconfig-base.json contains the following:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "declarationMap": true,
    "importHelpers": true,
    "inlineSources": true,
    "inlineSourceMap": true,
    "strictNullChecks": true,
    "noImplicitAny": false,
    "incremental": true,
    "noEmitOnError": true,
    "baseUrl": "./",
    "paths": {
      "*": ["node_modules/*"]
    }
  },
  "exclude": ["node_modules"]
}

packageA and packageC tsconfig.json:

{
  "extends": "../../../tsconfig-base.json",
  "compilerOptions": {
    "jsx": "react",
    "jsxFactory": "h",
    "outDir": "./dist",
    "baseUrl": "./"
  },
  "include": ["..."],
  "references": [
    {
      "path": "../packageB"
    }
  ]
}

Problem

packageC will find packageB exports and packageA will not.

Solution

There are three possible solutions for this:

  1. Re-order the references in the root tsconfig.json so that packageB is listed before packageA. This is not documented.

  2. Include a reference to packageB in packageA and packageC. The packageB tsconfig.json needs the now mandatory (tsc will complain if it's missing) composite property set to true.

  3. Ignore Add prepend to the root tsconfig.json packageB reference. Does not work for outDir: microsoft/TypeScript#26565

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment