我在typescript的.tsconfig中设置了路径别名,所以我的导入看起来更干净.

在我的代码中,当我try 像这样导入我的接口时

import { ApiResponse } from '@api';

埃斯林特抱怨:Unable to resolve path to module '@api'


相关文件

在my tsconfig.json中,我设置了如下路径别名:

{
  "compilerOptions": {
    "moduleResolution": "node",               
    "baseUrl": "./src",                     
    "paths": {                              
      "@api": ["./types/api"]
    },
  }
}

我的./src/types/api.ts是这样的:

// 3rd party API response object
export interface ApiResponse {
  ....
}

最后,我的.eslintrc.json分是这样的:

{
  "env": {
    "node": true
  },
  "globals": {
    "console": true
  },
  "plugins": ["@typescript-eslint", "prettier"],
  "parser": "@typescript-eslint/parser",
  "settings": {
    "import/extensions": [".js", ".ts"],
    "import/parsers": {
      "@typescript-eslint/parser": [".ts"]
    },
    "import/resolver": {
      "node": {
        "extensions": [".js", ".ts"]
      }
    }
  }
}


知道我做错了什么吗?

推荐答案

要支持tsconfig baseUrlpaths,需要软件包eslint-import-resolver-typescript.

  1. 确保具备以下基本知识:
# install the basics
npm i -D @typescript-eslint/parser @typescript-eslint/eslint-plugin

# support tsconfig baseUrl and paths
npm i -D eslint-plugin-import eslint-import-resolver-typescript
  1. 然后在eslintrc中,这里是json:
{
  "settings": {
    "import/resolver": {
      "typescript": {}
    }
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json",
    "tsconfigRootDir": "./"
  },
  "plugins": [
    "@typescript-eslint",
    "import"
  ],
  "extends": [
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking"
  ]
}

Typescript相关问答推荐

如果我有对象的Typescript类型,如何使用其值的类型?

是否可以根据嵌套属性来更改接口中属性的类型?

忽略和K的定义

使某些类型的字段变为可选字段'

React typescribe Jest调用函数

在键和值为ARGS的泛型函数中未正确推断类型

在NextJS中获得Spotify Oauth,但不工作'

将对象属性转换为InstanceType或原样的强类型方法

如何判断对象是否有重叠的叶子并产生有用的错误消息

为什么有条件渲染会导致material 自动完成中出现奇怪的动画?

表单嵌套太深

如何合并两个泛型对象并获得完整的类型安全结果?

TypeScrip:强制使用动态密钥

TypeScrip-根据一个参数值验证另一个参数值

递归生成常量树形 struct 的键控类型

Typescript泛型验证指定了keyof的所有键

如何限定索引值必须与相应属性的id字段相同

为什么 Typescript 无法正确推断数组元素的类型?

使用嵌套属性和动态执行时,Typescript 给出交集而不是并集

有没有办法将类型与关键更改相匹配?