microsoft / microsoft/TypeScript

Resolving hoisted `typeRoots` paths

Open
#33,183 9 comments 40 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.6.2

Search Terms:

  • resolve typeRoots in parent package node_modules
  • sharing tsconfig in yarn workspaces
  • use hoisted tsconfig in child package

Code

I've set up a Lerna monorepo which uses Yarn Workspaces. Within this monorepo is a config package, which other packages depend on. This config package has @types/node and @types/jest as dependencies.

From sibling packages, the shared config is used like so:

{
  "extends": "@project/config/ts.json",
  "include": ["src", "tests"],
  "compilerOptions": {
    "outDir": "lib"
  }
}

In the shared config, I point to the appropriate types root:

{
  "compilerOptions": {
    // ...
    "typeRoots": ["node_modules/@types"]
  }
}

However, within the shared config package, @types/* have been hoisted from the local node_modules to the monorepo's top-level node_modules. This behavior is usually fine given node's resolution... but it seems the config does not resolve to its parent's node_modules.

Expected behavior:

For the specified type root to resolve to the parent's node_modules

Actual behavior:

Tests fail with messages such as Cannot find name 'describe'. Do you need to install type definitions for a test runner? ...

One workaround:

{
  "compilerOptions": {
    // ...
    "typeRoots": ["../../node_modules/@types"]
  }
}

This workaround isn't very clean. Lerna & Yarn definitely pose some complexity for type root resolution. Hopefully resolving parents is considered a worthwhile modification.

Please let me know. Thank you :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Lerna and Yarn Workspaces setup with TypeScript 3.6.2 or typescript@next, using the shared config's typeRoots and the sibling package's extends configuration. Start by tracing how the relative node_modules/@types path is resolved from the shared config, then verify that hoisted parent type definitions are found and the missing describe errors no longer occur.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.