Repository: TypeScript
Issue: Automatic type package resolving doesn’t respect paths defined
Labels: Needs Investigation

<!-- 🚨 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:**  3.8.0-dev.20191105

<!-- Search terms you tried before logging this (so others can find this issue more easily) -->
**Search Terms:**
mangled scope package resovle

**Code**

Consider a project layout as follow:
```
$ tree -L 2
.
├── test.ts
├── third_party
│   ├── node_modules
│   ├── package.json
│   └── yarn.lock
└── tsconfig.json
```

test.ts
```ts
import * as babel from '@babel/core';
```

tsconfig.json
```json
{
  "compilerOptions": {
    "baseUrl": "./",
    "moduleResolution": "node",
    "outDir": "tsc-out",
    "paths": {
      "*": [
        "*",
        "third_party/node_modules/*",
      ],
    },
    "rootDir": "./",
    "strict": true,
    "traceResolution": true,
  },
  "include": [
    "*.ts",
  ],
}
```

package.json
```
{
  "devDependencies": {
    "@babel/core": "^7.7.0",
    "@types/babel__core": "^7.1.3",
    "typescript": "^3.8.0-dev.20191105"
  }
}
```

**Expected behavior:**

Code compiles without an error.

**Actual behavior:**

Code failed to compile.

```
$ ./third_party/node_modules/typescript/bin/tsc -p .
======== Resolving module '@babel/core' from '/Users/xiaoyi/Projects/tsc-mangle-resolve/test.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
'baseUrl' option is set to '/Users/xiaoyi/Projects/tsc-mangle-resolve', using this value to resolve non-relative module name '@babel/core'.
'paths' option is specified, looking for a pattern to match module name '@babel/core'.
Module name '@babel/core', matched pattern '*'.
Trying substitution '*', candidate module location: '@babel/core'.
Loading module as file / folder, candidate module location '/Users/xiaoyi/Projects/tsc-mangle-resolve/@babel/core', target file type 'TypeScript'.
Trying substitution 'third_party/node_modules/*', candidate module location: 'third_party/node_modules/@babel/core'.
Loading module as file / folder, candidate module location '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core', target file type 'TypeScript'.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core.ts' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core.tsx' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core.d.ts' does not exist.
Found 'package.json' at '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/package.json'.
'package.json' does not have a 'typesVersions' field.
'package.json' does not have a 'typings' field.
'package.json' does not have a 'types' field.
'package.json' has 'main' field 'lib/index.js' that references '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js'.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' exist - use it as a name resolution result.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' has an unsupported extension, so skipping it.
Loading module as file / folder, candidate module location '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js', target file type 'TypeScript'.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js.ts' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js.tsx' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js.d.ts' does not exist.
File name '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' has a '.js' extension - stripping it.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.ts' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.tsx' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.d.ts' does not exist.
Directory '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' does not exist, skipping all lookups in it.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/index.ts' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/index.tsx' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/index.d.ts' does not exist.
Loading module '@babel/core' from 'node_modules' folder, target file type 'TypeScript'.
Directory '/Users/xiaoyi/Projects/tsc-mangle-resolve/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'babel__core'
Directory '/Users/xiaoyi/Projects/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'babel__core'
Directory '/Users/xiaoyi/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'babel__core'
Directory '/Users/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'babel__core'
Directory '/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'babel__core'
'baseUrl' option is set to '/Users/xiaoyi/Projects/tsc-mangle-resolve', using this value to resolve non-relative module name '@babel/core'.
'paths' option is specified, looking for a pattern to match module name '@babel/core'.
Module name '@babel/core', matched pattern '*'.
Trying substitution '*', candidate module location: '@babel/core'.
Loading module as file / folder, candidate module location '/Users/xiaoyi/Projects/tsc-mangle-resolve/@babel/core', target file type 'JavaScript'.
Trying substitution 'third_party/node_modules/*', candidate module location: 'third_party/node_modules/@babel/core'.
Loading module as file / folder, candidate module location '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core', target file type 'JavaScript'.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core.js' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core.jsx' does not exist.
Found 'package.json' at '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/package.json'.
'package.json' does not have a 'typesVersions' field.
'package.json' has 'main' field 'lib/index.js' that references '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js'.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' exist - use it as a name resolution result.
======== Module name '@babel/core' was successfully resolved to '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' with Package ID '@babel/core/lib/index.js@7.7.0'. ========
test.ts:1:24 - error TS7016: Could not find a declaration file for module '@babel/core'. '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' implicitly has an 'any' type.
  Try `npm install @types/babel__core` if it exists or add a new declaration (.d.ts) file containing `declare module '@babel/core';`

1 import * as babel from '@babel/core';
                         ~~~~~~~~~~~~~


Found 1 error.
```

`@babel/core` doesn't ship with typedefs, so `tsc` tries to find the typedef in `@types`. After trying `@types/@babel/core`, `tsc` decides to try the mangled scope package name `@types/babel__core`. However, the second attempt no longer follows the `paths` defined in `tsconfig.json`.

**Playground Link:** <!-- A link to a TypeScript Playground "Share" link which demonstrates this behavior -->

N/A

**Related Issues:** <!-- Did you find other bugs that looked similar? -->

#19104, similar, but adding `node_modules/@types/*` won't work for mangled scope package name case.