Port TypeScript PR #62844: Gate `#/` subpath imports on NodeNext and Bundler
Port microsoft/TypeScript PR #62844 - Gate #/ subpath imports on NodeNext and Bundler module resolution modes
This PR aligns with Node.js PR nodejs/node#60864, which allows defining symmetric exports and imports fields in package.json:
{
"exports": { "./*": "./src/*" },
"imports": { "#/*": "./src/*" }
}
Changes
- Added
NodeResolutionFeaturesImportsPatternRootflag (bit 6) to gate#/root wildcard imports - Updated
NodeNextDefaultandBundlerDefaultto include the new flag, whileNode16Defaultexcludes it - Modified
loadModuleFromImports()to check feature flag before rejecting#/patterns - Added compiler test cases from the original TypeScript PR to verify the behavior
Test Coverage
Added two test cases:
nodeModulesPackageImportsRootWildcard.ts- Tests that#/imports work in NodeNext modenodeModulesPackageImportsRootWildcardNode16.ts- Tests that#/imports are rejected in Node16 mode
Behavior
| Mode | #/ imports |
|---|---|
| Node16 | Rejected (backward compatible) |
| NodeNext | Allowed |
| Bundler | Allowed |
Imports like #cjs or #utils (not starting with #/) continue to work in all modes.
Original prompt
Port https://github.com/microsoft/TypeScript/pull/62844
Custom agent used: Strada to Corsa Port Expert A Go and TypeScript expert who can easily figure out how to port PRs from one language to another
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
Sad there's no test but it's correct, so
@copilot also copy the compiler testcases from the original PR. Do not port the fourslash test
Added the compiler test cases from the original PR in commit 400b4d7. The tests verify that:
#/patterns work correctly in NodeNext mode (resolves to the mapped path)#/patterns are rejected in Node16 mode with error TS2307
Did not port the fourslash test as requested.
A bit sad I didn't notice that the original PR didn't just use one test with
node16,nodenext,bundler, but, oh well
Thanks for the feedback! I intentionally separated the error cases into a distinct test file because I thought it would make the error baselines cleaner and easier to read. But now I'm wondering if that separation was actually necessary. For future reference, would it be better to combine them into a single test with node16,nodenext,bundler? I'd appreciate any guidance!
Yeah, it makes separate baselines, so is a pretty good solution to this.