[英]ESLint: Why is Symbol not defined (no-undef rule)?
在我最近設置的 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"
]
}
在我的 babelrc 中,我有以下內容:
{
"presets": [
[
"@babel/preset-env"
],
["@babel/preset-typescript"]
],
"plugins": [
"@babel/plugin-transform-modules-commonjs",
[
"@babel/plugin-transform-runtime",
{
"corejs": 2
}
]
]
}
為什么會發生這種情況,我該如何解決這個問題?
如果在 "parserOptions" 下設置"ecmaVersion": 2018
,則"parserOptions"
僅支持 ES2018語法。 對於像Symbol
這樣的 ES6 全局變量,你想指定env
(如果上面沒有指定,則自動啟用 ES6 語法支持):
.eslintrc.json:
{ "env": { "es6": true } }
看看他們的 文檔:
同理,支持 ES6 語法與支持新的 ES6 全局變量(例如,Set 等新類型)是不一樣的。 對於 ES6 語法,使用
{ "parserOptions": { "ecmaVersion": 6 } };
對於新的 ES6 全局變量,使用{ "env": { "es6": true } }
。{ "env": { "es6": true } }
自動啟用 ES6 語法,但{ "parserOptions": { "ecmaVersion": 6 } }
不會自動啟用 ES6 全局變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.