小编A-S*_*A-S的帖子

noFallthroughCasesInSwitch - 明确允许失败

  1. noFallthroughCasesInSwitch我已在 tsconfig.json 文件中启用该选项。
  2. 该选项警告我有一个“错误”,我想让 Typescript 编译器知道这是故意的。
  3. 它没有记录,并且在线示例对我不起作用 - 我如何将其标记为故意的?
function getRandomInt(max: number) {
  return Math.floor(Math.random() * max);
}

switch(getRandomInt(3)) {
  /* falls through */
  /* fall through */
  /* FALLTHROUGH */
  case 1: /* falls through */ /* fall through */ /* FALLTHROUGH */ /* <----- Still getting an error here "Fallthrough case in switch. (7029)" */
    /* falls through */
    /* fall through */
    /* FALLTHROUGH */
    console.log(1);
    /* falls through */
    /* fall through */
    /* FALLTHROUGH …
Run Code Online (Sandbox Code Playgroud)

switch-statement typescript

10
推荐指数
2
解决办法
2万
查看次数

Typescript:抽象泛型类的子类类型

我有一个基本泛型类:

abstract class BaseClass<T> {
  abstract itemArray: Array<T>;

  static getName(): string {
    throw new Error(`BaseClass - 'getName' was not overridden!`);
  }

  internalLogic() {}
}
Run Code Online (Sandbox Code Playgroud)

和继承人:

type Item1 = {
  name: string
}
class Child1 extends BaseClass<Item1> {
  itemArray: Array<Item1> = [];
  static getName(): string {
    return "Child1";
  }
}


type Item2 = {
  name: number
}
class Child2 extends BaseClass<Item2> {
  itemArray: Array<Item2> = [];
  static getName(): string {
    return "Child2";
  }
}
Run Code Online (Sandbox Code Playgroud)

现在我想要定义一个以继承者作为其值的对象:

type IChildrenObj = {
  [key: string]: …
Run Code Online (Sandbox Code Playgroud)

javascript abstract-class typescript

7
推荐指数
1
解决办法
4178
查看次数

eslint“未找到规则‘no-explicit-any’的定义”始终显示

使用启动了一个react/typescript项目react-scripts,添加了一个.eslintrc.json包含以下内容的文件:

{
    "env": {
        "browser": true,
        "es2021": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:react/recommended",
        "plugin:@typescript-eslint/recommended",
        "react-app",
        "react-app/jest"
    ],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 12,
        "sourceType": "module"
    },
    "plugins": [
        "react",
        "@typescript-eslint"
    ],
    "rules": {
        "import/no-anonymous-default-export": "off",
        "no-explicit-any": "error",
        "@typescript-eslint/no-extra-semi": "off",
        "@typescript-eslint/no-explicit-any": "error",
        "@typescript-eslint/no-unused-vars": "off",
        "@typescript-eslint/explicit-module-boundary-types": "error",
        "@typescript-eslint/no-non-null-assertion": "off",
        "@typescript-eslint/no-inferrable-types": "off",
        "@typescript-eslint/no-empty-interface": "off"
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,由于某种我不知道的原因,每个文件都以 warning/error: 开头Line 1:1: Definition for rule 'no-explicit-any' was not found no-explicit-any,即使文件甚至没有类型any,即使文件是空的! …

typescript eslint

2
推荐指数
1
解决办法
5906
查看次数