Repository: TypeScript
Issue: `typeVersions` substitution occurs multiple times causing resolution to fail
Labels: Needs Investigation,Rescheduled

<!-- 🚨 STOP 🚨 STOP 🚨 STOP 🚨

Half of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common "Bugs" That Aren't Bugs section!

Please help us by doing the following steps before logging an issue:
  * Search: https://github.com/Microsoft/TypeScript/search?type=Issues
  * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ

Please fill in the *entire* template below.
-->

<!--
Please try to reproduce the issue with the latest published version. It may have already been fixed.
For npm: `typescript@next`
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly
-->
**TypeScript Version:**  4.0.3, 4.1.0-dev.20201027

When module direcotry is imported which is mapped to typedefs through `typeVersions` typescript ends up doing path substitution multiple times and ends up with a wrong lookup path leading to failed resolution. I have created minimal reproducible example https://github.com/Gozala/bug-tsc-path-substitution/tree/double-substitution

Where you can see that `src/index.ts` imports `bar/src` module which seems to do the following:

1. typesVersion substitution is applied to 'src', which matched pattern '*'
   > Trying substitution 'dist/*', candidate module location: 'dist/src'.
2. Which triggers following lookups:
  - `node_modules/bar/dist/src.ts`
  - `node_modules/bar/dist/src.tsx`
  - `node_modules/bar/dist/src.d.ts`
3. After neither found instead of looking up at `node_modules/bar/dist/src/index.(ts,tsx,d.ts)` it seems to look at 'package.json' 'main' field and do **second path substitution**:
   > 'package.json' has 'main' field 'src/index.js' that references 'node_modules/bar/dist/src/src/index.js'.
4. Which fails because it ends up looking for `dist/src/dist/src/index` instead of `dist/src/index`.
5. This is especially problematic because typescript seems to generate import paths like `bar/src` in place of `bar` as illustrated by `node_modules/foo/` where original import was for `bar` but typedefs emitted have `bar/src` instead (for no apparent reason)


<!-- Search terms you tried before logging this (so others can find this issue more easily) -->
**Search Terms:**
- module resolution
- typeVersions
- double substitution

**Code**

- [main.ts](https://github.com/Gozala/bug-tsc-path-substitution/blob/7b4e5e0a7c7cb2981cc55e18e6769bd90009528a/src/main.ts)
- [bar/src/index](https://github.com/Gozala/bug-tsc-path-substitution/blob/7b4e5e0a7c7cb2981cc55e18e6769bd90009528a/node_modules/bar/dist/src/index.d.ts)


**Expected behavior:**

After considering following paths:

  - `node_modules/bar/dist/src.ts`
  - `node_modules/bar/dist/src.tsx`
  - `node_modules/bar/dist/src.d.ts`

Following paths should be considered

  - `node_modules/bar/dist/src/index.ts`
  - `node_modules/bar/dist/src/index.tsx`
  - `node_modules/bar/dist/src/index.d.ts`


**Actual behavior:**

Instead after considering following paths

After considering following paths:

  - `node_modules/bar/dist/src.ts`
  - `node_modules/bar/dist/src.tsx`
  - `node_modules/bar/dist/src.d.ts`

TS seems to do another path substitute for `dist/src/index.js` ends up with wrong path `node_modules/bar/dist/src/dist/src/index.js` which cases failure in finding `dist/src/index.d.ts`

**Playground Link:** <!-- A link to a TypeScript Playground "Share" link which demonstrates this behavior -->

Can't demonstrate with playground, as many files are involved. Please see demo repo instead
https://github.com/Gozala/bug-tsc-path-substitution/tree/double-substitution

**Related Issues:** <!-- Did you find other bugs that looked similar? -->

- https://github.com/microsoft/TypeScript/issues/41281
- As mentioned earlier generated typedefs seems to add `/src` to otherwise `bar` imports (which don't exhibit this problem). Which on it's own seems surprising making this a lot more problematic.