问题:TypeScript找不到声明文件

qkf9rpyu  于 2023-04-22  发布在  TypeScript
关注(0)|答案(1)|浏览(197)

我在/src/models/中有两个文件,分别是User.ts和User. d. ts。我试图在User中构建一个类,然后为我在User. d. ts中使用的对象声明一个接口。我以为User.ts可以自动使用接口,因为typescript会解析所有的d.ts文件?配置有问题吗?或者我只是不理解这个概念?
我得到的错误在User.d.ts文件中:

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: src/models/User.d.ts.
The file must be included in at least one of the projects provided

User.ts:

class User {
  private id: number;

  constructor(id: number) {
    this.id = id;
  }

  static create(userData: UserData): User | undefined {
    return undefined;
  }

  getId(): number {
    return this.id;
  }
}

export default User;

UserData.d.ts:

interface UserData {
  id?: number;
  gmail?: string;
  firstName?: string;
  lastName?: string;
  loginIP?: string;
  secureKey?: string;
  imgFileName?: string; // file name of the users profile image
  lastUpdate?: string;
  createDate?: string;
}

用户.ts在查找UserData时遇到问题,我似乎无法在文件中使用它。我的tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true
  },
  "include": ["/src/**/*.ts", "**/src/**/*.ts", "**/__tests__/**/*.ts"]
}

我的.eslintrc.js

module.exports = {
  extends: ['airbnb', 'plugin:@typescript-eslint/recommended'],
  parser: '@typescript-eslint/parser',
  plugins: ['@typescript-eslint', 'prettier'],
  settings: {
    'import/parsers': {
      '@typescript-eslint/parser': ['.ts', '.tsx'],
    },
    'import/resolver': {
      typescript: {},
    },
  },
  rules: {
    'import/no-extraneous-dependencies': [2, { devDependencies: ['**/*.test.tsx', '**/*.test.ts'] }],
    '@typescript-eslint/indent': [2, 2],
    'import/extensions': [
      'error',
      'ignorePackages',
      {
        js: 'never',
        jsx: 'never',
        ts: 'never',
        tsx: 'never',
        mjs: 'never',
      },
    ],
  },
};

我该怎么办?谢谢你的帮助。谢谢。

2vuwiymt

2vuwiymt1#

TLDR

tsconfig.json中配置typeRoots配置

答案1

在我看来,你可以在tsconfig.json中使用typeRoots。这个配置:

"compilerOptions": {
    ...
    "typeRoots" : ["./typings"]
    ...
 },

答案二

如何使用"/src/**/*.d.ts"修复include选项

"include": ["/src/**/*.ts", "**/src/**/*.ts", "**/__tests__/**/*.ts",  "/src/**/*.d.ts"]

参考

  1. Typescript Handbook

相关问题