【问题标题】:Typescript paths not resolving打字稿路径无法解析
【发布时间】:2019-08-18 10:00:46
【问题描述】:

Here 的 Github MCVE 显示问题。 npm run compile 显示错误。

我正在尝试这样做:

import {Todo} from '@test';

但它没有解决。

src/index.ts:1:20 - 错误 TS2307:找不到模块“@test”。

我在tsconfig.json 中有paths。

  "baseUrl": "./",                          /* Base directory to resolve non-absolute module names. */
  "paths": {
    "@fs/": ["src/"], 
    "@test/": ["test/"]
  },                                        /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */

想法?

Github Typescript 问题

I opened a bug report for this here 因为根据某些 cmets 它应该可以工作。

Typescript 请注意,我更新了 github 存储库,删除了 @test 中的 glob 模式和斜线。

【问题讨论】:

  • In the docs 在路径映射下,他们将基本 url 设置为 .
  • @pmkro . 和./ 解析到同一个目录,相反会是一个TS实现的bug
  • @NinoFiliu 我想了很多

标签: javascript node.js typescript tsconfig


【解决方案1】:

删除全局模式:

"paths": {
  "@test": "./test"
}

您的解决方案应该可以正常工作,但 the docs 不使用 glob,there have been some reported intellisense bugs 在使用 glob 时。

【讨论】:

  • 嗯,是的,this article 也这么说。但是在我的机器上,删除 glob 模式使整个事情按预期工作,并且将它们添加回来引发了一个错误。这足以成为我的答案。
  • IMO 在我的 IDE 和 TS 的实现之间肯定存在一个错误,但是,如果修复对某人有效,它具有作为答案的价值
  • 我尝试删除 glob 模式,但它仍然无法解决我的问题。我将更改推送到 github 存储库。
  • 这个问题也与 VSCode 分开。我正在运行在package.json 中配置的npm run compile,并且src/index.ts:1:20 - error TS2307: Cannot find module '@test' ` 仍在记录中。
  • 你没有说我写的;)它是"@test": ["./test"],而不是"@test/": ["./test/"]。没有斜线。
【解决方案2】:

不要调整 VS 代码导入模块说明符设置(根据一些浮动的答案)。并且不要删除 glob 模式。事实上添加更多:

 "baseUrl": "./",
  "paths": {
    "@fs/*": ["src/*"], 
    "@test/*": ["test/*"]
  },

请注意,glob 位于键和值中。有时很难发现这一点。

如果目标是文件,则应从键和值中省略 glob。

【讨论】:

  • 你先生,太棒了。
猜你喜欢
  • 2018-07-30
  • 2020-09-21
  • 2019-03-22
  • 2016-08-22
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
  • 2019-04-16
相关资源
最近更新 更多