首页 > 解决方案 > 另一个 TS7006:参数“X”隐含具有“任何”类型——但不完全是

问题描述

我知道有很多与这个问题相关的问题/答案,据我所知,我理解它的本质,但我不明白为什么它会发生在我明确设置和控制的类型上。

我特意设置noImplicitAnytrue里面tsconfig.json;我知道如果我把它转向false这个问题就会消失 - 但我不想这样做......但是:)

这是代码:

# ./src/page/base.page.ts
export default class BasePage {
  public open(path: string) {
    browser.url(path);
  }
}

# ./src/page/home-search.page.ts
import BasePage from "./base.page";

class HomeSearchPage extends BasePage {
  public fillForm(term: string) {
    $("#search_form_input_homepage").setValue(term);
  }

  public open() {
    super.open(`${browser.options.baseUrl}`);
  }

  public submit() {
    $("#search_button_homepage").click();
  }
}

export default new HomeSearchPage();

# ./test/spec/search.engine.spec.ts
import HomeSearchPage from "@page/home-search.page";
import { expect } from "chai";

describe("DuckDuckGo (DDG) Search Engine", () => {
  it("Verify search result(s) for given term", () => {
    const expected: string = "WebdriverIO · Next-gen WebDriver test framework for Node.js";
    const input: string = "webdriverio";

    HomeSearchPage.open();
    HomeSearchPage.fillForm(input);
    HomeSearchPage.submit();

    expect($("div#r1-0 h2.result__title a.result__a").getText()).to.contain(expected);
  });
});

...最后,我的tsconfig.json(位于项目的根目录):

{
  "compilerOptions": {
    "alwaysStrict": true,
    "baseUrl": ".",
    "module": "commonjs",
    "paths": {
      "*": ["./*"],
      "@page/*": ["./src/page/*"]
    },
    "removeComments": true,
    "strict": true,
    "target": "es2018",
    "types": [
      "@wdio/mocha-framework",
      "@wdio/sync",
      "@types/chai",
      "@types/mocha",
      "node"
    ],

    //---------------------------------------------------------------------------------------------
    //  Experimental Settings
    //---------------------------------------------------------------------------------------------
    "noImplicitAny": true
  },
  "exclude": ["node_modules/"],
  "include": ["./src/**/*", "./test/**/*"]
}

使用该设置,我总是收到相同的错误消息:

[0-0] RUNNING in chrome - /test/spec/search.engine.spec.ts
0-0 worker error { name: 'TSError',
  message:
   '⨯ Unable to compile TypeScript:\nsrc/page/home-search.page.ts(5,14): error TS7006: Parameter \'term\' implicitly has an \'any\' type.\n',
  stack:
   'TSError: ⨯ Unable to compile TypeScript:\nsrc/page/home-search.page.ts(5,14): error TS7006: Parameter \'term\' implicitly has an \'any\' type.\n\n    at createTSError
...
at Module.load (internal/modules/cjs/loader.js:653:32)' }
[0-0] FAILED in chrome - /test/spec/search.engine.spec.ts

标签: typescripttypescript-typingswebdriver-io

解决方案


此错误来自ts-node. 在您的wdio.conf.js中,将您的更改before: function()为:

  before: function (capabilities, specs) {
    require("ts-node").register({ files: true, transpileOnly: true });
  },

这里的这个问题还提到了与您遇到的问题非常相似的问题。我在本地下载了你的 repo 并让测试成功运行。


推荐阅读