gpt4 book ai didi

typescript - 在 TypeScript 和 Next.js 中使用绝对导入解析模块

转载 作者:行者123 更新时间:2023-12-05 07:06:02 24 4
gpt4 key购买 nike

我一直在尝试消除在使用绝对导入从文件导入函数时不断出现的一些错误。我得到的错误如下所示。 enter image description here

函数其实都是导入的,可以正常使用。但是,每当使用绝对路径导入某些内容时,我的 typescript 配置都会使 VS 代码抛出类似的错误。我的理解是,从 Next.js 的第 9 版开始,typescript 开箱即用,无需做太多额外的工作。

我的 tsconfig.json 文件

{
"compilerOptions": {
"target": "esnext",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"strictNullChecks": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"sourceMap": true,
"baseUrl": ".",
"paths": {
"@actions/*": [
"redux/actions/*"
],
"@components/*": [
"./components/*"
],
"@controllers/*": [
"controllers/*"
],
"@interfaces/*": [
"interfaces/*"
],
"@layouts/*": [
"layouts/*"
],
"@lib/*": [
"lib/*"
],
"@models/*": [
"models/*"
],
"@options/*": [
"options/*"
],
"@public/*": [
"public/*"
],
"@reducers/*": [
"redux/reducers/*"
],
"@redux/*": [
"redux/*"
],
"@static/*": [
"static/*"
],
"@store/*": [
"./store/*"
],
"@style": [
"style/*"
],
"@utils": [
"utils/*"
],
"@dummy/*": [
"__dummy__/*"
]
}
},
"exclude": [
"node_modules"
],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
]
}

我的 next.config.js 文件

/* eslint-disable */
const withPlugins = require("next-compose-plugins")
const withFonts = require("next-fonts")
const withImages = require("next-images")
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin")
/* eslint-enable */

module.exports = withPlugins([[withFonts], [withImages]], {
webpack: (config) => {
if (config.resolve.plugins) {
config.resolve.plugins.push(new TsconfigPathsPlugin())
} else {
config.resolve.plugins = [new TsconfigPathsPlugin()]
}

config.resolve.extensions.push(".ts", ".tsx")
return config
}
})

最佳答案

如果您使用 Next.js 9.4,您可以删除 tsconfig-paths-webpack-plugin 插件。

Absolute Imports and Aliases

关于typescript - 在 TypeScript 和 Next.js 中使用绝对导入解析模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62614327/

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