在我最近设置的一个 Typescript 项目中,我让 Babel 来编译我的 Typescript 代码。我也@typescript-eslint
用作我的 linter。到目前为止,它一直运行良好,直到最近我尝试Symbol
在我的代码中使用它。
出于某种原因,Typescript(或 Babel)无法识别
Symbol
并给我一个错误,即Symbol is not defined
.
这是我的 eslintrc 的样子:
{
"parser": "@typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint/eslint-plugin"
]
}
Run Code Online (Sandbox Code Playgroud)
在我的 babelrc 中,我有以下内容:
{
"presets": [
[
"@babel/preset-env"
],
["@babel/preset-typescript"]
],
"plugins": [
"@babel/plugin-transform-modules-commonjs",
[
"@babel/plugin-transform-runtime",
{
"corejs": 2
}
]
]
}
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况,我该如何解决这个问题?
我想在 VS Code (Windows) 中使用 Fira Code,并且已经按照说明中的说明配置了字体。不知何故,字体看起来很模糊。我该如何解决这个问题?
"editor.fontFamily": "'Fira Code'",
"editor.fontLigatures": true,
Run Code Online (Sandbox Code Playgroud) 我正在使用 VS Code,我想知道是否有一种方法可以在类似于其他流行 IDE 的项目中快速创建新的类文件。目前我坚持手动创建新文件,命名它something.java
,并用必要的代码填充它。我已经安装了常见的 Java 扩展包(调试器、dep.viewer、语言支持、maven)。
有什么线索吗?也许是第三方扩展?
提前致谢
丹尼尔
在 React 功能组件中取消异步请求的正确方法是什么?
我有一个脚本,它在加载时(或在某些用户操作下)从 API 请求数据,但如果这是正在执行的过程中并且用户导航离开,则会导致以下警告:
警告:无法对卸载的组件执行 React 状态更新。这是一个空操作,但它表明您的应用程序中存在内存泄漏。要修复,请取消 useEffect 清理函数中的所有订阅和异步任务。
我读过的大部分内容都使用基于类的组件AbortController
的componentDidUnmount
方法解决了这个问题。而我的 React 应用程序中有一个功能组件,它使用 Axois 向 API 发出异步数据请求。
该函数驻留在useEffect
功能组件中的钩子中,以确保在组件呈现时运行该函数:
useEffect(() => {
loadFields();
}, [loadFields]);
Run Code Online (Sandbox Code Playgroud)
这是它调用的函数:
const loadFields = useCallback(async () => {
setIsLoading(true);
try {
await fetchFields(
fieldsDispatch,
user.client.id,
user.token,
user.client.directory
);
setVisibility(settingsDispatch, user.client.id, user.settings);
setIsLoading(false);
} catch (error) {
setIsLoading(false);
}
}, [
fieldsDispatch,
user.client.id,
user.token,
user.client.directory,
settingsDispatch,
user.settings,
]);
Run Code Online (Sandbox Code Playgroud)
这是触发的 axios 请求:
async function fetchFields(dispatch, clientId, token, folder) {
try { …
Run Code Online (Sandbox Code Playgroud) 我有一个函数,它接受一些值,并+
在它和一个2
值上执行运算符:
function myFunction(input: number): number {
return input + 2;
}
Run Code Online (Sandbox Code Playgroud)
如果我传递一个数字,它会将这个数字添加到2
:
const result = myFunction(2);
console.log('Result: ', result);
// Result: 2
Run Code Online (Sandbox Code Playgroud)
如果我传递一个字符串,它将将此字符串连接到2
:
const result = myFunction(");
console.log('Result: ', result);
// Result: "22"
Run Code Online (Sandbox Code Playgroud)
到目前为止一切都很好。现在我想使用泛型来捕获类型:
function myFunction<T>(input: T): T {
return input + 2;
}
Run Code Online (Sandbox Code Playgroud)
如果我想用它来捕获参数的隐式类型,我可以这样做:
const result = myFunction(2);
console.log('Result: ', result);
// Error: `(parameter) input: T. Operator '+' cannot be applied to types 'T' and '2'.`
Run Code Online (Sandbox Code Playgroud)
如您所见,TypeScript 返回有关类型和+
运算符的错误,我不明白为什么。如果我明确设置类型,则相同:
const …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Reactjs 来构建我的应用程序。当我运行我的应用程序时,出现以下错误。
后续变量声明必须具有相同的类型。变量 'WebGL2RenderingContext' 必须是类型 '{ new (): WebGL2RenderingContext; 原型:WebGL2RenderingContext;只读 ACTIVE_ATTRIBUTES:数字;只读 ACTIVE_TEXTURE:数字;只读 ACTIVE_UNIFORMS:数字;只读 ALIASED_LINE_WIDTH_RANGE:数字;... 554 更多...; 只读 WAIT_FAILED:数字;}',但这里有类型 '{ new (): WebGL2RenderingContext; 原型:WebGL2RenderingContext;只读 ACTIVE_ATTRIBUTES:数字;只读 ACTIVE_TEXTURE:数字;只读 ACTIVE_UNIFORMS:数字;只读 ALIASED_LINE_WIDTH_RANGE:数字;... 555 更多...; 只读 MAX_CLIENT_WAIT_TIMEOUT_WEBGL:数字;}'
包中使用了以下依赖项。
"@babel/core": "7.1.0",
"@babel/plugin-transform-react-jsx-source": "^7.0.0",
"@svgr/webpack": "2.4.1",
"@tensorflow/tfjs-converter": "^0.6.6",
"@tensorflow/tfjs-core": "^0.13.10",
"@types/elasticsearch": "^5.0.34",
"@types/http-aws-es": "^6.0.0",
"@types/lodash": "^4.14.118",
"@types/react": "^16.7.6",
"@types/react-dom": "^16.0.9",
"@types/react-router-dom": "^4.3.3",
"@types/react-webcam": "^1.1.0",
"@types/socket.io-client": "^1.4.32",
"antd-mobile": "^2.2.13",
"aws-sdk": "^2.476.0",
"axios": "^0.19.0",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "9.0.0",
"babel-jest": "23.6.0",
"babel-loader": "8.0.4",
"babel-plugin-named-asset-import": "^0.2.3",
"babel-preset-react-app": "^6.1.0",
"bfj": "6.1.1",
"case-sensitive-paths-webpack-plugin": …
Run Code Online (Sandbox Code Playgroud)