gpt4 book ai didi

javascript - Webpack 找不到 Typescript 类型解析所需的导入。如何解决这个问题?

转载 作者:行者123 更新时间:2023-12-01 15:43:47 25 4
gpt4 key购买 nike

我的项目使用 typescript 并使用 webpack 构建。
我有一个来自位于 node_modules/@types/libname/custom.d.ts 下的第三方库的打字文件.该文件有一个命名空间声明,其中包含几种类型:

declare namespace MyNamespace {
export type A = ...;
}
然后为了能够在我的 typescript 代码中使用该类型,如下所示:
const x: MyNamespace.A;
...我需要在应用程序的某处添加如下导入声明:
import 'libname'
哪个 tsc 正确解析为 node_modules/@types/libname/custom.d.ts所以如果我用 tsc 编译一切正常。
但是 webpack 无法构建。它无法理解这个导入:
ERROR in ./Index.tsx
Module not found: Error: Can't resolve 'libname' in 'C:\<path_to_index_file>'
@ ./Index.tsx 44:0-32
我试图在我的 webpack.config 中添加一个别名,如下所示:
    resolve: {
alias: Object.assign({
'libname$': path.resolve(__dirname, 'node_modules/@types/libname/custom.d.ts'),
}
},
但随后它失败了,因为它无法理解该文件的语法并询问我是否缺少加载程序。
我该如何解决这个问题,以便 webpack 能够理解这个typings import 语句?
我使用的是 webpack 版本 4.41.6谢谢!

最佳答案

您应该使用 null-loader对于这个导入,webpack 会忽略它。

关于javascript - Webpack 找不到 Typescript 类型解析所需的导入。如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62681940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com