当在tsconfig.json中设置baseUrl
时,编译器将不会查看baseUrl/@types
以尝试查找类型定义
TypeScript版本: 2.2.1
代码
示例可在:
https://github.com/spion/ts-base-url-issue 找到
如果从tsconfig.json中删除"paths"条目,编译器将无法找到react-dom
预期行为:
即使没有"paths"条目,也能正常编译。
实际行为:
如果删除了路径配置选项,编译器找不到定义文件。--trace-resolution
显示它从未尝试过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.
解决方法
添加一个路径条目将@types
目录再次放入查找列表中:
"baseUrl": "./vendor/node_modules",
"paths": {
"*": ["@types/*", "*"]
}
5条答案
按热度按时间vuktfyat1#
设置
baseUrl
告诉编译器,所有非相对模块名称都在baseUrl
下,这也适用于您的外部依赖项,如react-dom
。您添加的路径Map对我来说似乎是正确的。
z0qdvdin2#
在这种情况下,将以下内容添加到:https://www.typescriptlang.org/docs/handbook/module-resolution.html
移动node_modules似乎是一个常见的需求,现在我们有了
@types
,仅更改baseUrl是不起作用的。gr8qqesn3#
目前的状态是:
设置baseUrl告诉编译器在哪里找到模块。所有非相对名称的模块导入都被认为是相对于baseUrl的。
不确定我们是否需要声明
@types\module
只是另一个模块。oogrdqng4#
我相信一个“移动node_modules”的例子是有意义的,因为它相当常见(在Docker中使用外部只读文件系统的watch模式,官方的Rails Webpacker类型支持使用vendor/node_modules等)。
这种行为当然不是最直观的,因为人们认为
baseUrl/node_modules
和baseUrl/node_modules/@types
也会被查找。我认为这可能会节省人们一两个小时的时间😀
bxfogqkk5#
我同意。我认为baseUrl就像是定义目录的根(就像tsconfig.json所在的位置)。到目前为止,两个大惊喜是:我不得不指向node_modules,而且找不到node_modules/@types。