繁体   English   中英

TypeScript:“Window & typeof globalThis”上不存在属性“X”:使用“declare global”的建议解决方案给了我错误

[英]TypeScript: Property 'X' does not exist on 'Window & typeof globalThis': suggested solution using 'declare global' gives me error

编辑:这是一个 ESlint 问题。 我在这篇文章的末尾包含了我的 ESLint 设置。

我对 TypeScript 还很陌生——我一直在重构别人的 React 代码,当我遇到这一行时,将它移到 TypeScript 以用于工作项目

        const memoryOptionsResponse = window.webchatMethods.getMemory(chatId);

TypeScript 抱怨“window.webchat”,说

“Window & typeof globalThis”上不存在属性“X”

经过一番谷歌搜索后, 答案似乎是将其放在模块的根级别

declare global {
    interface Window {
        webchatMethods:any;
    }
}

但是现在 TS 抱怨以下错误。

这个 Stack Overflow 页面提出了类似的建议,但它再次declare global和 ESLint。

在尝试解决这个问题半小时后,我决定向您寻求建议——如果有人知道一个好的解决方案,我将不胜感激!

在此处输入图片说明

// .eslintconfig
{
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 2020,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "modules": true
    }
  },
  "extends": [
    "eslint:recommended",
    "prettier",
    "plugin:react/recommended"
  ],
  "plugins": [
    "prettier",
    "react",
    "react-hooks"
  ],
  "env": {
    "es6": true,
    "node": true,
    "mocha": true,
    "browser": true
  },
  "rules": {
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn",
    "react/prop-types": 1,
    "react/jsx-uses-react": "error",
    "react/jsx-uses-vars": "error",
    "react/no-unescaped-entities": "warn",
    "react/no-find-dom-node": 0,
        "comma-dangle": ["error", "never"],
    "global-require": 0
  }
}

// .eslintignore
test
node_modules

看起来缺少的是告诉 babel 如何处理打字稿的部分。 我会检查typescript-eslint

暂无
暂无

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

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