Skip to content

Changing baseUrl causes compiler not to look at @types subdirectory #14551

@spion

Description

@spion

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/*", "*"]
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsThe issue relates to how you learn TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions