繁体   English   中英

如何在tsconfig中为单个模块使用路径?

[英]How to use paths in tsconfig for a single module?

这个问题是如何在tsconfig.json中使用路径的后续文章? 除了我想为一个模块做。

我有一个模块:

  • 它在src/functions/foo.ts
  • 其内容是:

     export default interface Bar { } 
  • 它由其他模块使用非相对路径导入:

     import * as Foo from "foo" 

编译器找不到它:

error TS2307: Cannot find module 'foo'

这个tsconfig不能解决这个问题...

{ "compilerOptions": { "noEmit": true, "strict": true, "module": "commonjs", "target": "es2017", "noImplicitAny": true, "moduleResolution": "node", "sourceMap": true, "outDir": "build", "baseUrl": ".", "paths": { "foo": ["src/functions/*"], "*": [ "node_modules/*" ] } }, "include": [ "./src/**/*", "./typings/**/*", "./test/**/*", "./test-integration/**/*" ] }

...但是这样做:

"paths": { "*": [ "node_modules/*", "src/functions/*" ] }


为什么paths的第一个版本不起作用-我做错了什么,我该怎么做才能确保仅在导入foo时才使用"src/functions/*" (而不是在导入* )?

(我在带有Node.js的Windows上使用tsc版本3.1.6)。

您正在将单词foo分配给目录src/functions/*

但是像这样的foo只能用于指定单个文件(模块)的确切位置,而不能使用通配符,因此,如下所示:

"paths": {
    "foo": ["src/functions/foo"],
    "*": [
        "node_modules/*"
    ]
}

您可能正在寻找的是

"paths": {
    "foo/*": ["src/functions/*"],
    "*": [
        "node_modules/*"
    ]
}

foo/*代替foo

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM