ESLint:在declare模块中只允许声明和类型导入



我尝试将其添加到我的接口.ts文件中,但ESLint没有:

declare module 'react' {
interface HTMLAttributes<T> extends AriaAttributes, DOMAttributes<T> {
// extends React's HTMLAttributes
name?: string;
ref?: HTMLAnchorElement;
cssClass?: string;
}
}

ESLint分析错误:`只允许声明和类型导入

我在images.d.ts中的这段代码中也遇到了同样的错误,它不喜欢const关键字:

declare module '*.png' {
const value: any;
export = value;
}

是不是每个人都把这种代码放在自定义的.d.ts文件中,并告诉esint忽略它?

内部声明模块`.eslintrc

{
"extends": [
"airbnb",
"eslint:recommended",
"plugin:react/recommended",
"plugin:import/errors"
],
"plugins": ["react"],
"parser": "babel-eslint",
"parserOptions": {"ecmaFeatures": {"jsx": true}},
"env": {"mocha": true},
"rules": {"comma-dangle": 0,
"max-classes-per-file": [0],
"jsx-a11y/href-no-hash": [0],
"complexity": ["error", {"max": 6}],
"handle-callback-err": "error",
"import/default": [0],
"import/order": [0],
"max-statements": ["error", 30],
"no-console": [0],
"no-constant-condition": "error",
"no-param-reassign": ["error", {"props": false}],
"no-process-exit": "error",
"no-useless-call": "error",
"react/no-danger": [0],
"react/jsx-indent-props": [0],
"react/jsx-filename-extension": [1, {"extensions": [".tsx"]}],
"react/jsx-indent": [0],
"react/jsx-tag-spacing": ["error", { "beforeSelfClosing": "always" }],
"react/jsx-sort-props": ["error", {"ignoreCase": false, "noSortAlphabetically": false}],
"react/prefer-stateless-function": [0, { "ignorePureComponents": true }],
"react/no-multi-comp": [0, { "ignoreStateless": true }],
"react/require-optimization": [0],
"react/prop-types": [0],
"react/no-array-index-key": [1],
"semi": ["error", "always"],
"import/no-extraneous-dependencies": [0],
"react/destructuring-assignment": [0],
"react/jsx-closing-bracket-location": [1, "after-props"],
"jsx-one-expression-per-line": [0],
"import/extensions": [0],
"import/no-unresolved": [0],
"no-multiple-empty-lines": [0],
"one-var": [0],
"max-len": ["error", { "code": 140 }],
"implicit-arrow-linebreak": [0],
"no-shadow": [0],
"function-paren-newline": [0],
"no-tabs": [0],
"indent": [1, "tab"],
"no-use-before-define": [0],
"no-nested-ternary": [1],
"no-restricted-syntax": [1],
"no-plusplus": [1],
"react/no-find-dom-node": [1]
}
}

有两种方法可以解决这个

  1. ESLint认为.d.ts文件是.js

    将它们添加到您的.eslintignore文件中,这将为您解决问题:(

    /**/*.d.ts
    
  2. 添加此包yarn add --dev @typescript-eslint/parser

    参考:GitHub链接

    并添加到你的.eslintrc

    module.exports = {
    ...
    parserOptions: {
    parser: "@typescript-eslint/parser"
    },
    

最新更新