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

How to use paths in tsconfig for a single module?

这个问题是 的后续问题,除了我想为单个模块做这个问题。

我有一个模块:

编译器没有找到:

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/*" (而不是在导入时 *)?

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

您正在将一个词 foo 分配给目录 src/functions/* 的内容。

但是像这样的foo只能用来指定单个文件(模块)的准确位置,没有通配符,所以,像这样:

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

您可能正在寻找的是

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

(foo/* 而不是 foo)