如何为ESLint设置项目的根目录?

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

How to set root of project for ESLint?

问题

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

Next.js可以很好地解释它,不会报错,但是ESLint会。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.html
匿名

发表评论

匿名网友

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

确定