Skip to content

Feature Request: Allow for special tokens to be used in paths in tsconfig.json #45050

@D4N14L

Description

@D4N14L

Suggestion

Add special tokens that can be used for paths in tsconfig.json, similar to Jest usage of the <rootDir> tag.

🔍 Search Terms

tsconfig, extends, node, resolution, configuration

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Add special tokens that can be used for paths in tsconfig.json, similar to Jest usage of the <rootDir> tag. This would be done to allow for complete extends functionality when extending a dependency

📃 Motivating Example

TypeScript tsconfig.json files have the ability to extend from other tsconfig.json files through the use of Node-resolved packages. This has the benefit of making extending from base tsconfig files contained in a dependency quite easy and clean. However, all path-based options within tsconfig.json (like include, exclude, etc.) are pathed relative to the dir of the tsconfig.json (see docs: https://www.typescriptlang.org/tsconfig#include). This is impossible because using Node resolution for the extends field returns the real path of the tsconfig, which in some package managers (pnpm, Yarn) may not be directly under the node_modules folder. This makes it practically impossible to include path-based options in a base tsconfig since they will always need to be re-defined in the extending config to re-root the paths.

💻 Use Cases

The only current workaround is to use relative pathing when using the extends field, since this maintains the resolution structure and allows for base tsconfig files to specify paths that are resolved up through the node_modules folder, ex:

tsconfig.json:

{
  "extends": "./node_modules/my-package/tsconfig-base.json"
}

my-package/tsconfig-base.json:

{
  // Traverse up through the `node_modules/my-package` paths to get to the src of the extending project
  "include": [ "../../src" ]
}

If special tokens are implemented, this allows for pathing into different dirs that would otherwise be inaccessible to the base tsconfig, ex:

tsconfig.json:

{
  "extends": "my-package/tsconfig-base.json",
  "rootDir": "."
}

my-package/tsconfig-base.json:

{
  // Use the <rootDir> token to reference the <rootDir> of the combined tsconfig
  "include": [ "<rootDir>/src" ]
}

<rootDir> however is just an example, and other (better) options likely exist. The general idea is to allow for referencing the top-level tsconfig file when extending a base tsconfig file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions