Typescript baseUrl not working in React Typescript project

孤街醉人 提交于 2021-02-11 04:31:35

问题


I defined the baseUrl in my tsconfig.json file:

"baseUrl": "src",

In .eslintrc.js I have:

parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2018,
    sourceType: 'module',
    project: './tsconfig.json',
  },

Now in e.g. index.tsx I can import my components like import Layout from 'components/layout';

When I run gatsby develop I get some errors like:

If you're trying to use a package make sure that 'components/layout' is installed. If you're trying to use a local file make sure that the path is correct.
error undefined failed

What's missing here, why it isn't finding my components?


回答1:


You have to add import/resolver settings in your .eslintrc.js so Webpack can identify imports of your components (I'm supposing that they have .tsx extension)

settings: {
    'import/resolver': {
      node: {
        paths: [
          'src'
        ],
        extensions: [
          '.ts',
          '.tsx'
        ]
      }
    }
  },


来源:https://stackoverflow.com/questions/65540603/typescript-baseurl-not-working-in-react-typescript-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!