简体   繁体   English

问题:TypeScript 找不到声明文件

[英]Issue: TypeScript not finding the declaration file

I have two files in /src/models/ they are User.ts and User.d.ts.我在 /src/models/ 中有两个文件,它们是 User.ts 和 User.d.ts。 I am trying to build a class in User and then have a interface declaration for an object I use in User.d.ts.我正在尝试在 User 中构建一个 class,然后为我在 User.d.ts 中使用的 object 进行接口声明。 I thought User.ts would be able to use the interface automatically because typescript parses all the d.ts files?我认为 User.ts 将能够自动使用该接口,因为 typescript 解析所有 d.ts 文件? Is there something wrong with the config?是不是配置有问题? Or maybe I am just not understanding the concept?或者也许我只是不理解这个概念?

The Error I get is in the User.d.ts file:我得到的错误在 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:用户.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:用户数据.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;
}

User.ts is having an issue finding UserData and I can't seem to use it in the file. User.ts 在查找 UserData 时遇到问题,我似乎无法在文件中使用它。 My tsconfig.json:我的 tsconfig.json:

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

My.eslintrc.js我的.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',
      },
    ],
  },
};

Where am I off?我去哪儿了? Appreciate the help.感谢帮助。 Thanks.谢谢。

TLDR TLDR

How about use typesroot config in tsconfig.jsontsconfig.json中使用typesroot配置怎么样

Answer 1答案 1

The way I see it, you can use typesroot in tsconfig.json .在我看来,您可以在tsconfig.json typesroot This config这个配置

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

Answer 2答案 2

How about fix include options with "/src/**/*.d.ts"如何使用"/src/**/*.d.ts"修复include选项

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

Refernce参考

  1. Typescript Handbook Typescript 手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM