打字稿路径无法解析

时间:2019-03-27 18:10:02

标签: javascript node.js typescript tsconfig

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

我正在尝试这样做:

import {Todo} from '@test';

但这并没有解决。

  

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

我在paths中有tsconfig.json

  "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打字稿问题

I opened a bug report for this here,因为根据某些评论它应该可以正常工作。

打字稿说明,我更新了github存储库,删除了@test中的glob模式和斜杠。

2 个答案:

答案 0 :(得分:2)

删除glob模式:

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

您的解决方案应该可以正常工作,但是the docs不使用全局变量,而there have been some reported intellisense bugs不使用全局变量。

答案 1 :(得分:1)

请勿调整VS代码导入模块说明符设置(根据一些答案)。并且不要删除全局模式。实际上,在其中添加了更多内容:

# doesn't work as expected, as the variable i in the function call
# is evaluated only after the loop is run
x <- list()
for (i in 1:3) {
  x[[i]] <- function() {i}
}
x[[1]]()
#> [1] 3
x[[2]]()
#> [1] 3
x[[3]]()
#> [1] 3

# by writing a function generator, we can bind the variable i
# to the specific function we're generating in each iteration
# of the loop
x <- list()
f_generator <- function(i) {
  force(i)
  function() {i}
}
for (i in 1:3) {
  x[[i]] <- f_generator(i)
}
x[[1]]()
#> [1] 1
x[[2]]()
#> [1] 2
x[[3]]()
#> [1] 3

请注意,全局变量位于键AND值中。有时候很难发现这一点。

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

相关问题