English 中文(简体)
宣布变数为构造者时的 Es
原标题:Eslint bug when declaring variables into constructor TS

这是我用字典写成的法典:

class Snake {
constructor(
    public x: number,       // error  x  is defined but never used
    public y: number,       // error  y  is defined but never used
    public size = 10,       // error  size  is assigned a value but never used
    public color = (() => { 
        const tab = new Array(6).fill(0);
        return `#${tab.map(() => (Math.random() * 0xF << 0).toString(16)).join(  )}`;
    })(),                   // error  color  is assigned a value but never used
) { }

update() {
    const coef = (): number => {
        const nb = Math.round(Math.random());
        return nb === 0 ? -1 : 1;
    };
    this.x += Math.random() * 10 * coef();
    this.y += Math.random() * 10 * coef();
}
...

I got eslint installed and it tells me for all the variables : is defined but never used and right under it I use them. I think eslint doesn t understand the declaration inside the constructor parentheses but I m not sure. I use this method of declaration very often so if I can fix this it I ll be happy

{
"env": {
    "browser": true,
    "es2021": true
},
"extends": [
    "airbnb-base"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
    "ecmaVersion": "latest",
    "sourceType": "module"
},
"plugins": [
    "@typescript-eslint"
],
"rules": {
    "no-restricted-globals": "off",
    "consistent-return": "off", 
    "no-return-assign": "off", 
    "prefer-const": "off", 
    "no-param-reassign": "off", 
    "block-scoped-var": "off", 
    "no-use-before-define": "off", 
    "no-undef": "warn", 
    "no-unused-vars": "warn", 
    "no-plusplus": "off", 
    "no-var": "off", 
    "vars-on-top": "off", 
    "indent": [
        "error",
        4
    ], 
    "no-console": "off", 
    "no-mixed-spaces-and-tabs": "off", 
    "linebreak-style": "off", window
    "class-methods-use-this": "off", 
    "no-empty": "off", 
    "no-constant-condition": "off", 
    "nonblock-statement-body-position": [
        "error",
        "below"
    ], 
    "curly": "off", 
    "no-useless-constructor": "off",
    "no-empty-function": "off",
    "no-bitwise": "off"
}

这里是我的先辈,错误来自未使用过的瓦尔斯。

问题回答

以下线有错误:

"linebreak-style": "off", window

<代码>window

应当:

"linebreak-style": "off",

否则,从今天看,lin星座就显得无效。

之所以存在这一问题,是因为ESLint与规则相违背(应当予以撤销):

 no-unused-vars :  off ,

因此,规则表格@typescript-eslint/eslint-plugin 应当使用原始材料:

 typescriptEslintPlugin/no-unused-vars :  error ,

That s it. Here is example flat config for ESLint 9 wtih this setup:

import eslintJsPlugin from  @eslint/js ;
import typescriptEslintPlugin from  @typescript-eslint/eslint-plugin ;
import typescriptParser from  @typescript-eslint/parser ;
import globals from  globals ;

export default [
  // global config, applied to all configurations
  {
    files = [ **/*.ts ],
    languageOptions: {
      ecmaVersion:  latest ,
      sourceType:  module ,
      parser: typescriptParser,
      globals: {
        ...globals.node,
      },
    },
  },
  // recommended config with rules
  eslintJsPlugin.configs.recommended,
  {
    rules: {
       no-unused-vars :  off ,
    },
  },
  // configs for plugins with overridden rules
  {
    plugins: {
      typescriptEslintPlugin,
    },
    rules: {
      ...typescriptEslintPlugin.configs[ eslint-recommended ].rules,
       typescriptEslintPlugin/explicit-function-return-type :  error ,
       typescriptEslintPlugin/no-unused-vars :  error ,
    },
  },
];

值得补充的是,2024 Q2 @typescript-eslint/eslint-plugin。 页: 1

这个员额是用户@Bergi





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签