How to set root of project for ESLint?

huangapple go评论66阅读模式
英文:

How to set root of project for ESLint?

问题

为了澄清,我的项目是使用Next.js和Typescript构建的。我一直以这种方式导入我的函数:import Component from "/components/Component/Component";,其中/指的是/src文件夹(这样我就不必使用相对导入来导入所有函数)。

Next.js很好地解释了它,并没有抛出错误,但是ESLint却抛出了错误:Cannot find module '/path/to/component' or its corresponding type declarations. ts(2307)。这真的让我很恼火,因为项目看起来有48个错误,而其中80%的错误只是来自这一个错误。

有没有办法可以强制ESLint完全忽略typescript错误(TS2307)?

提前感谢!

英文:

For clarification, my project is built with Next.js and Typescript. I've been importing my functions this way: import Component from "/components/Component/Component";, where / refers to the /src folder (so I don't have to import all the functions with relative imports).

Next.js interprets it nicely and doesn't throw me an error, but ESLint does. For every line ESLint throws the error: Cannot find module '/path/to/component' or its corresponding type declarations. ts(2307). It really infuriates me because the project looks like it has 48 errors while 80% of those errors are just from that one error.

Is there any way I can force ESLint to completely ignore the typescript error (TS2307)?

Thanks in advance!

答案1

得分: 0

这不是常见的做法,从根目录开始导入。我建议在tsconfig.json中添加baseUrl属性(如果还没有的话)。

"baseUrl": "./"

之后,你可以像这样导入你的组件:

import Component from "components/Component/Component";

英文:

It is not really common practice to start import from /. I would suggest adding baseUrl property to tsconfig.json if you don't have it already

"baseUrl": "./"

After that you can import your components like this

import Component from "components/Component/Component";

huangapple
  • 本文由 发表于 2023年1月8日 22:43:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048661-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定