microsoft / microsoft/TypeScript

Changing baseUrl causes compiler not to look at @types subdirectory

Open
#14,551 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Docs
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

When baseUrl is set in tsconfig.json, the compiler will not look into baseUrl/@types to try and find type definitions

TypeScript Version: 2.2.1

Code

Example is available at:

https://github.com/spion/ts-base-url-issue

If the "paths" entry is removed from tsconfig.json, the compiler is unable to find react-dom

Expected behavior:

Compiles fine even without a "paths" entry.

Actual behavior:

Compiler doesn't find the definition file if the paths config option is removed. --trace-resolution shows that it never tries baseUrl/@types/react-dom

======== Resolving module 'react-dom' from '/Users/spion/Documents/tests/ts-modules-broken/app/index.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
'baseUrl' option is set to '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules', using this value to resolve non-relative module name 'react-dom'
Resolving module name 'react-dom' relative to base url '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules' - '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom'.
Loading module as file / folder, candidate module location '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom', target file type 'TypeScript'.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom.ts' does not exist.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom.tsx' does not exist.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom.d.ts' does not exist.
Found 'package.json' at '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/package.json'.
'package.json' does not have a 'typings' field.
'package.json' does not have a 'types' field.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.ts' does not exist.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.tsx' does not exist.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.d.ts' does not exist.
Loading module 'react-dom' from 'node_modules' folder, target file type 'TypeScript'.
Directory '/Users/spion/Documents/tests/ts-modules-broken/app/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/spion/Documents/tests/ts-modules-broken/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/spion/Documents/tests/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/spion/Documents/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/spion/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
'baseUrl' option is set to '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules', using this value to resolve non-relative module name 'react-dom'
Resolving module name 'react-dom' relative to base url '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules' - '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom'.
Loading module as file / folder, candidate module location '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom', target file type 'JavaScript'.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom.js' does not exist.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom.jsx' does not exist.
Found 'package.json' at '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/package.json'.
'package.json' has 'main' field 'index.js' that references '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.js'.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.js' exist - use it as a name resolution result.
======== Module name 'react-dom' was successfully resolved to '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.js'. ========
app/index.ts(1,22): error TS7016: Could not find a declaration file for module 'react-dom'. '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.js' implicitly has an 'any' type.

Workaround

Adding a paths entry gets the @types directory into the lookup list again:

    "baseUrl": "./vendor/node_modules",
    "paths": {
      "*": ["@types/*", "*"]
    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked ts-base-url-issue reproduction, especially tsconfig.json and app/index.ts, and run the compiler with --trace-resolution. Compare resolution with and without the paths entry, then trace the module-resolution entry point responsible for declaration lookup. Done means baseUrl/@types/react-dom is searched without requiring a paths configuration and the reproduction compiles successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.