Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
D4N14L opened this issue Jul 15, 2021 · 1 comment
Closed
5 tasks done
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@D4N14L
Copy link
Member

D4N14L commented Jul 15, 2021

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.

@D4N14L D4N14L changed the title Allow for special tokens to be used in paths in tsconfig.json Feature Request: Allow for special tokens to be used in paths in tsconfig.json Jul 15, 2021
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jul 16, 2021
@everett1992
Copy link

There are compiler options that you can not reasonably set in a shared tsconfig file.

I always want to compile src into dist but I can't include outDir, rootDir, or includes in a shared config, because typescript resolves them relative to the shared config rather than relative to the configured package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants