Skip to content

Fix TS2742 false positive for self-imports in declaration files#62843

Closed
zaewc wants to merge 5 commits intomicrosoft:mainfrom
zaewc:fix-self-import-62806-clean
Closed

Fix TS2742 false positive for self-imports in declaration files#62843
zaewc wants to merge 5 commits intomicrosoft:mainfrom
zaewc:fix-self-import-62806-clean

Conversation

@zaewc
Copy link
Copy Markdown

@zaewc zaewc commented Dec 5, 2025

Fixes #62806

This PR fixes an issue where self-referencing imports inside .d.ts files incorrectly resolve to source files during tsc --build. This caused false TS2742 errors because inferred types appeared to reference non-portable paths in the source tree instead of the emitted declaration files.

Problem

During project reference builds, self-imports such as:

// packages/c/src/other.d.ts
import { C } from "c";

resolve "c" to src/C.ts instead of dist/C.d.ts.

This results in:
- False positive TS2742 errors
- Incorrect parentSpecifiers pointing to source directories
- Behavior that occurs only under --build, not regular tsc

Solution

A post-resolution step was added to resolveModuleName to correct self-imports in .d.ts files:
1. If the importer is a .d.ts file and the resolved module is a TS source file:
2. Walk upwards to locate the nearest package.json for the importer
3. If the module name matches the package name (indicating a self-import):
4. Recompute the expected .d.ts output path using outDir/rootDir
5. Redirect the resolved module to the correct emitted .d.ts file

This redirection only applies to .d.ts importers and does not affect normal source file resolution.

Tests

Added tests in src/testRunner/unittests/tsbuild/moduleResolution.ts verifying that:
- A self-import inside src/other.d.ts resolves to dist/index.d.ts instead of src/index.ts
- The build produces no TS2742 errors
- Trace resolution logs show the redirected path
Removing the fix causes the test to fail, confirming coverage

@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Dec 5, 2025
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Dec 5, 2025
@zaewc
Copy link
Copy Markdown
Author

zaewc commented Dec 5, 2025

@microsoft-github-policy-service agree

@zaewc zaewc force-pushed the fix-self-import-62806-clean branch from e482e97 to 7d331a1 Compare December 6, 2025 00:36
@github-project-automation github-project-automation bot moved this from Not started to Done in PR Backlog Mar 24, 2026
@typescript-bot
Copy link
Copy Markdown
Collaborator

With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies).

Next steps for PRs:

  • For crash bugfixes or language service improvements, PRs are currently accepted at the typescript-go repo
  • Changes to type system behavior should wait until after 7.0, at which point mainline TypeScript development will resume in this repository with the Go codebase
  • Library file updates (lib.d.ts etc) continue to live in this repo or the DOM Generator repo as appropriate

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

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Self-referencing causes non-portable inferred types (TS2742) false positive

4 participants