Cer*_*ean 7 typescript reactjs
我对 TypeScript 还很陌生——我一直在重构别人的 React 代码,当我遇到这一行时,将其移至 TypeScript 以用于工作项目
const memoryOptionsResponse = window.webchatMethods.getMemory(chatId);
Run Code Online (Sandbox Code Playgroud)
TypeScript 抱怨“window.webchat”,说
“Window & typeof globalThis”上不存在属性“X”
经过一番谷歌搜索后,答案似乎是将其放在模块的根级别
declare global {
interface Window {
webchatMethods:any;
}
}
Run Code Online (Sandbox Code Playgroud)
但是现在 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
}
}
Run Code Online (Sandbox Code Playgroud)
// .eslintignore
test
node_modules
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3344 次 |
| 最近记录: |