tsconfig 路径不起作用

新手上路,请多包涵

我正在尝试做一些与文档中的 jquery 路径示例 非常相似的事情,但是 TS 不断抛出 TS2307 (webpack 编译良好):

 "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
        "@client": [
            "client",
        ],
        "@suir": [
            "../node_modules/semantic-ui-react/dist/commonjs", // not working
        ],
    },
    // …
},
"include": [
    "*.d.ts",
    "client/**/*",
    "../node_modules/semantic-ui-react", // is this necessary?
],

Changing baseUrl to "." and updating includes and paths makes no difference ( @client continues to work and @suir 继续不起作用)。

"@suir" 更改为 "@suir/""@suir/*" (并附加 /* 值也没有区别。


我这样做的原因是为了简化我的导入(我明确指定它们而不是从包中提取命名导出以减少我的供应商包大小 - 节省大约 1 MB):

 import Button from 'semantic-ui-react/dist/commonjs/elements/Button'; // works

import Button from '@suir/elements/Button'; // not found

原文由 Jakob Jingleheimer 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.8k
2 个回答

我不知道为什么这在我第 11 次尝试(但不是第 10 次)中起作用,但 /* 似乎是秘诀,文档中的示例显然是指向到特定文件(并且省略了文件扩展名)。

 {
    "compilerOptions": {
        "baseUrl": "./src", // setting a value for baseUrl is required
        "moduleResolution": "node", // was not set before, but is the default
        "paths": {
            "@client/*": [
                "client/*",
            ],
            "@suir/*": [ // notice the `/*` at the end
                "../node_modules/semantic-ui-react/dist/commonjs/*", // notice the `/*`
            ],
        },
        // …
    },
    "include": [
        "./src/client/**/*",
    ],
}

原文由 Jakob Jingleheimer 发布,翻译遵循 CC BY-SA 4.0 许可协议

确保提供的路径正确。它将抛出错误“未找到模块及其声明”。

原文由 Ummer Zaman 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进