gpt4 book ai didi

typescript - 问题 : TypeScript not finding the declaration file

转载 作者:行者123 更新时间:2023-12-05 00:45:31 27 4
gpt4 key购买 nike

我在/src/models/中有两个文件,它们是 User.ts 和 User.d.ts。我正在尝试在 User 中构建一个类,然后为我在 User.d.ts 中使用的对象声明一个接口(interface)。我认为 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

用户.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;

用户数据.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 在查找 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',
},
],
},
};

我在哪里?感谢帮助。谢谢。

最佳答案

TLDR

How about use typesroot config in tsconfig.json

回答 1

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

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

回答 2

如何使用 "/src/**/*.d.ts"

修复 include 选项
"include": ["/src/**/*.ts", "**/src/**/*.ts", "**/__tests__/**/*.ts",  "/src/**/*.d.ts"]

引用

  1. Typescript Handbook

关于typescript - 问题 : TypeScript not finding the declaration file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63317922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com