Nicolas SEPTIER
Nicolas SEPTIER

Reputation: 851

Typescript/yarn workspaces - Referenced project may not disable emit

I'm trying to setup a monorepo project using typescript and yarn workspaces.

The project's structure looks like this:

/example
/packages
  /lib1

Example is an app that used the packages, for development purposes.

When I use yarn tsc --build --force I get the following error:

example/tsconfig.json:6:18 - error TS6310: Referenced project '/packages/intro' may not disable emit.

Here's example's tsconfig.json:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "./lib"
  },
  "references": [{ "path": "../packages/intro" }]
}

And the one at the project's root:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "React Native",
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "composite": true,
    "declaration": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "jsx": "react-native",
    "lib": ["es2017"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmit": true,
    "paths": {
      "my-project/*": ["./packages/*/src"]
    },
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "esnext"
  },
  "exclude": [
    "packages/*/lib",
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ],
  "references": [{ "path": "./example" }, { "path": "./packages/lib1" }]
}

And the lib1's tsconfig.json:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "./lib"
  }
}

As you can see I do have noEmit set to true so I don't understand what the error is about. I've tried setting the value directly in each tsconfig files but that didn't do anything as expected.

Upvotes: 26

Views: 27040

Answers (3)

Gabriel Pureliani
Gabriel Pureliani

Reputation: 353

You can use either noEmit or emitDeclarationOnly fields individually without having to specify the other, to fix your issue - try replacing noEmit with emitDeclarationOnly, e.g:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "React Native",
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "composite": true,
    "declaration": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "jsx": "react-native",
    "lib": ["es2017"],
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDeclarationOnly": true, // use this field instead of "noEmit"
    "paths": {
      "my-project/*": ["./packages/*/src"]
    },
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "esnext"
  },
  "exclude": [
    "packages/*/lib",
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ],
  "references": [{ "path": "./example" }, { "path": "./packages/lib1" }]
}

sources:
https://www.typescriptlang.org/tsconfig#noEmit
https://www.typescriptlang.org/tsconfig#emitDeclarationOnly

Upvotes: 7

Khasky
Khasky

Reputation: 130

Workaround based on zerdox's comment:

Just add the following:

"files": [],

to your root tsconfig.json

Example:

{
  "files": [],
  "references": [
    {
      "path": "packages/types"
    }
  ]
}

Upvotes: -1

Mal Curtis
Mal Curtis

Reputation: 695

Setting noEmit to true disables emitting, which is what the error is saying. You need to set noEmit to false to enable emitting.

Upvotes: -8

Related Questions