Skip to content

Allow subpath imports that start with #/#62844

Merged
andrewbranch merged 2 commits intomicrosoft:mainfrom
magic-akari:feat/issue-62841
Dec 9, 2025
Merged

Allow subpath imports that start with #/#62844
andrewbranch merged 2 commits intomicrosoft:mainfrom
magic-akari:feat/issue-62841

Conversation

@magic-akari
Copy link
Copy Markdown
Contributor

@magic-akari magic-akari commented Dec 6, 2025

This aligns with Node.js PR nodejs/node#60864, which allows defining symmetric exports and imports fields in package.json:

{
  "exports": { "./*": "./src/*" },
  "imports": { "#/*": "./src/*" }
}

Fixes #62841

This aligns with Node.js PR nodejs/node#60864, which allows defining
symmetric `exports` and `imports` fields in package.json:

{
  "exports": { "./*": "./src/*" },
  "imports": { "#/*": "./src/*" }
}
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Dec 6, 2025
@magic-akari magic-akari marked this pull request as ready for review December 6, 2025 04:03
Copilot AI review requested due to automatic review settings December 6, 2025 04:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enables TypeScript to support subpath imports starting with #/* in package.json, aligning with Node.js behavior. Previously, TypeScript rejected these patterns, but they are now valid according to Node.js PR nodejs/node#60864.

  • Removed restriction that blocked #/* import patterns
  • Added comprehensive test coverage for the new functionality

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated no comments.

File Description
src/compiler/moduleNameResolver.ts Removed check that prevented #/* subpath imports
tests/cases/conformance/node/nodeModulesPackageImportsRootWildcard.ts Added test case validating #/* imports across ESM/CJS modules
tests/cases/fourslash/importCompletionsPackageJsonImportsPatternRootWildcard.ts Added test for import completions with #/* pattern
tests/baselines/reference/nodeModulesPackageImportsRootWildcard(module=). Generated baseline files for various module configurations

@Scalahansolo
Copy link
Copy Markdown

Does this implementation handle multiple file extensions? I.e. if a project contains both .ts and .tsx files?

@jakebailey
Copy link
Copy Markdown
Member

I had assumed this would require a new module resolution mode.

@andrewbranch

@andrewbranch
Copy link
Copy Markdown
Member

Yeah, technically this shouldn’t go in node16. @magic-akari can you gate this on moduleResolution === ModuleResolutionKind.NodeNext?

@Scalahansolo
Copy link
Copy Markdown

Shouldn't this also work with moduleResolution being bundler?

@magic-akari
Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! I'll gate this feature on NodeNext and Bundler modes. For Bundler, since it doesn't need to strictly align with Node.js versions, I think it makes sense to include it as well.

One question: since this feature was just merged into Node.js (nodejs/node#60864), do we need to introduce a new module resolution mode (e.g., Node24) to represent the updated Node.js behavior? Or is gating it behind NodeNext sufficient for now?

@andrewbranch
Copy link
Copy Markdown
Member

I think gating it is fine for now.

@andrewbranch
Copy link
Copy Markdown
Member

Actually, it sounds like it will be backported as far back as v20, so probably we’ll want to snap the versioned module resolution at --moduleResolution node20, since we have --module node20 and selecting that should enable this. But we’ll want to wait for those backports to happen before merging anything versioned.

@typescript-bot typescript-bot added For Milestone Bug PRs that fix a bug with a specific milestone and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Dec 9, 2025
@github-project-automation github-project-automation bot moved this from Not started to Needs merge in PR Backlog Dec 9, 2025
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Dec 9, 2025
@andrewbranch andrewbranch added this pull request to the merge queue Dec 9, 2025
Merged via the queue into microsoft:main with commit 3b9acae Dec 9, 2025
33 checks passed
@github-project-automation github-project-automation bot moved this from Needs merge to Done in PR Backlog Dec 9, 2025
@Scalahansolo
Copy link
Copy Markdown

Excuse my ignorance, but for a PR like this that is marked for TS 6.0, when / what is the process for this landing in TS 7?

@RyanCavanaugh
Copy link
Copy Markdown
Member

Great question! It needs to be ported now. I started microsoft/typescript-go#2330 and we'll see how that goes

@magic-akari magic-akari deleted the feat/issue-62841 branch December 11, 2025 06:45
Copy link
Copy Markdown

@akhilsmokie7-cloud akhilsmokie7-cloud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm install -D typescript@beta

@danciudev
Copy link
Copy Markdown

I can't get VS Code's autocomplete to work with subpath imports. Is it possible that it doesn't support this yet?

@amanmahajan7
Copy link
Copy Markdown

Sorry if this is obvious — with TypeScript 6.0 supporting package.json#imports (including #/ subpath imports), does this mean we can fully replace tsconfig.paths with imports?

Or does TypeScript still require matching paths in tsconfig.json for type checking?

@andrewbranch
Copy link
Copy Markdown
Member

Subpath imports have been supported for type checking since TypeScript 4.7—since then, if your moduleResolution setting is node16/nodenext/bundler, you've never needed to replicate your Node.js-valid package.json "imports" into tsconfig.json paths. Node.js 20+ and TypeScript 6.0+ just expand the valid syntax a little bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Module Resolution: allow subpath imports that start with #/

10 participants