typescript - 另一个 TS7006:参数“X”隐含具有“任何”类型——但不完全是
问题描述
我知道有很多与这个问题相关的问题/答案,据我所知,我理解它的本质,但我不明白为什么它会发生在我明确设置和控制的类型上。
我特意设置noImplicitAny
到true
里面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
解决方案
此错误来自ts-node
. 在您的wdio.conf.js
中,将您的更改before: function()
为:
before: function (capabilities, specs) {
require("ts-node").register({ files: true, transpileOnly: true });
},
这里的这个问题还提到了与您遇到的问题非常相似的问题。我在本地下载了你的 repo 并让测试成功运行。
推荐阅读
- angular - Firebase + Capacitor Ionic Angular:@angular/fire 分析模块不起作用
- typescript - 如何根据 Typescript 中的字符串实例化一个类?
- php - 尽管有可用的语言环境,但 setlocale 失败 [已解决]
- python-3.x - 将对象(包含字符串和整数)Pandas 数据帧转换为 scipy 稀疏矩阵
- flutter - Flutter 为对象添加属性
- r - 使用 R 中的点和颜色为不同条件创建散点图
- java - 管理员更改用户密码后出现 IncorrectCredentialsException
- python - PyCharm 不强制从具有具体类型参数的通用抽象类继承
- node.js - 在 Node + Express API 中检测到的打开句柄会阻止 Jest 退出
- graphql - 文本字段与服务器的双向同步 - 快速输入