microsoft / microsoft/TypeScript

Add option to include default typeRoots when overriding typeRoots

Open
#30,855 23 comments 264 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Search Terms

default typeRoots

Suggestion

By default, when typeRoots is unspecified, TypeScript looks for node_modules/@types in the project's directory and all ancestor directories, similar to Node's module resolution algorithm. This is covered in the tsconfig.json docs and is implemented here.

When specifying typeRoots in tsconfig.json, this overrides the default typeRoots entirely. You can include "node_modules/@types" in your typeRoots array, but that does not include ancestor directories.

This is a feature request to provide a way, such as a new option, to include the default type roots in addition to the overridden values.

Use Cases

The primary use case is within a repository using Yarn workspaces. Yarn hoists npm packages to the workspace root. This means you can end up with a directory hierarchy like this:

.
├── node_modules
│   └── @types
│       └── dependency
└── packages
    └── example
        ├── example.ts
        ├── package.json
        └── tsconfig.json

If tsconfig.json wants to include node_modules/@types in the workspace root (in addition to other type declaration directories specified in typeRoots), it needs to look like this:

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

Examples

This feature request is a quality-of-life improvement to instead allow for:

{
  "compilerOptions": {
    "typeRoots": [...],
    "includeDefaultTypeRoots": true
  }
}

The default value of this option would be false and would not be a breaking change.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

Start with src/compiler/moduleNameResolver.ts around the linked lines and review the tsconfig.json documentation describing default typeRoots resolution. Define how an opt-in setting combines explicitly listed roots with ancestor node_modules/@types directories, then add coverage for the workspace hierarchy and confirm the default behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.