Search code examples
reactjstypescripttsconfig

Unable to find type definitions when using typescript 3.0.1 project refferences


I initially had following project structure (monorepo)

_app/
_firebase/
_types/
tsconfig.json

Here, tsconfig at root level was used for _app and _firebase typescript projects. _types is a folder containing files like models.ts, store.ts etc... these files just define different interfaces.

I was able to use these interfaces without importing / exporting anything in both _app and _firebase projects before with no isssues.

I made a change to use typescripts v3 project refferences, so my app structure now looks like this

_app/
  tsconfig.json
_firebase/
  tsconfig.json
_types/
tsconfig.json

Root level tsconfig.json has following contents

{
  "compilerOptions": {
    "composite": true
  },
  "references": [
    { 
      "path": "_app" 
    }
  ]
}

Other configs just have simple options like outDir, target, lib etc...

Now if I use interfaces from _types folder anywhere in _app or _firebase I get errors like

[ts] Cannot find name 'IInventoryModel'.

Is there any way I can preserve previous functionality where I was able to use these interfaces without explicit exports / imports while still taking advantage of new projects refferences feature?

EDIT: here is example of tsconfig for _app

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "lib": ["es2016"],
    "jsx": "react",
    "outDir": "./dist",
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
  }
}

EDIT 2 interfaces inside _types folder are declared like this i.e. _types/models.ts has

interface ICharacterModel { /* ... */ }

interface IInventoryModel { /* ... */ }

// etc...

Solution

  • For starters, _types needs its own tsconfig.json and _app needs to reference it. Then, based on the documentation, it looks like the magic step is to set an outFile on _types. The following configuration is working for me:

    // _app/tsconfig.json
    {
      "compilerOptions": {
        "composite": true,
        "target": "es2016",
        "module": "commonjs",
        "lib": ["es2016"],
        "jsx": "react",
        "outDir": "./dist",
        "strict": true,
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
      },
      "references": [
        {
          "path": "../_types"
        }
      ]
    }
    
    // _types/tsconfig.json
    {
        "compilerOptions": {
            "composite": true,
            "outFile": "types.js"
        }
    }