Another TS7006: Parameter "X" implicitly has an "any" type – but not quite
Another TS7006: Parameter "X" implicitly has an "any" type – but not quite
我知道有很多 questions/answers 与这个问题相关,据我所知,我理解它的本质,但我不明白为什么它会发生在我明确指出的类型上设置和控制。
我特意在tsconfig.json
里面把noImplicitAny
设置成了true
;我知道如果我把它变成 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
此错误来自 ts-node
。在您的 wdio.conf.js
中,将您的 before: function()
更改为:
before: function (capabilities, specs) {
require("ts-node").register({ files: true, transpileOnly: true });
},
也提到了一个与您遇到的问题非常相似的问题。我在本地拉下了你的回购并成功地进行了 运行 的测试。
我知道有很多 questions/answers 与这个问题相关,据我所知,我理解它的本质,但我不明白为什么它会发生在我明确指出的类型上设置和控制。
我特意在tsconfig.json
里面把noImplicitAny
设置成了true
;我知道如果我把它变成 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
此错误来自 ts-node
。在您的 wdio.conf.js
中,将您的 before: function()
更改为:
before: function (capabilities, specs) {
require("ts-node").register({ files: true, transpileOnly: true });
},