microsoft / microsoft/TypeScript

Allow paths to be masked and captured by regex

Open
#29,245 5 comments 47 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

Suggestion

Sometimes if we use a monorepo we will construct our monopackages like this:

packages
 - a
  - src/
    - index.ts
  - tsconfig.json
  - package.json
 - b
  - src/
    - index.ts
  - tsconfig.json
  - package.json
 - c
  - src/
    - index.ts
    - hello/
      - world.ts
  - tsconfig.json
  - package.json

So when we need to eject it as a git repo it is trivially done. However, I noticed that TypeScript path resolution is pretty limited to just simple replacement, how it is not working will be talked about later below. And I believe my examples are easier than my explanations :(

Scenario

Using babel-plugin-module-resolver, it was easy as pie:

const moduleResolver = [
  'babel-plugin-module-resolver', {
    'root': ['.'],
    'alias': {
      '^@app\/([^\/]+)\/?(.+)?': './packages/\\1/src/\\2'
    }
  }
]

But with the paths option of typescript, which only does simple substitution and is very useless that it could only accommodate one pattern star, is obviously much weaker in expressing power and cannot resolve context dependent subdirectories/submodules.

Examples

Consider this tsconfig

// Trying to use a common pattern to match "@app/{a,b,c}":
"paths": {
  "@app/*": ["packages/*/src"]
}
// Fine, what about "@app/c/hello/world"? It is actually substituted as packages/c/hello/world/src! 
// Doing a brute-force construction:
"paths": {
  "@app/a*": ["packages/a/src", "packages/a/src*"],
  "@app/b*": ["packages/b/src", "packages/b/src*"],
  "@app/c*": ["packages/c/src", "packages/c/src*"],
}
// It works but it wasn't elegant...consider if you have hundreds of monopackages and this would be hell
// Only if we could using a regex like pattern in babel-plugin-module-resolver:
"paths": {
  "^@app\/([^\/]+)\/?(.+)?": ["./packages/\\1/src/\\2"]
}
// Ah, much easier

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.

Similar issues

https://github.com/Microsoft/TypeScript/issues/27298
https://github.com/Microsoft/TypeScript/issues/26787

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 the issue's tsconfig paths examples and compare the requested regex captures with TypeScript's current path substitution behavior. Define how regex patterns and captured paths should interact with existing paths rules, then verify that monorepo imports such as @app/c/hello/world resolve to the intended package source path.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
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.