按照http://devexpress.github.io/testcafe/documentation/getting-started/上的 Testcafe 入门指南,我在修改示例测试脚本时遇到了麻烦。
打字稿版本:2.7.2 节点版本:8.7.0 Webstorm 版本:2017.2.6
import {Selector} from 'testcafe';
fixture `Getting Started`
.page `http://some-website.com`;
test('Test', async t => {
})
这编译并运行良好。我读过 Typescript 支持字符串模板,并且示例测试已经使用反引号来包围它的字符串,所以我尝试分解出一部分 URL:
import {Selector} from 'testcafe';
let domain = 'some-website.com';
fixture `Getting Started`
.page `http://${domain}`;
test('Test', async t => {
})
这会导致错误“TS2554:Expected 1 arguments, but got 2”,并且 webstorm 会突出显示以fixture(
.
尝试.page
用变量完全替换参数会导致不同的编译错误。
import {Selector} from 'testcafe';
let domain:string = 'premierrange-local.com';
let str:string = `http://${domain}`;
fixture `Getting Started`
.page str;
test('Test', async t => {
})
“TS1005:';' 预期的。” 关于str
变量的使用。
但是,放入str
括号可以解决编译错误:
import {Selector} from 'testcafe';
let domain:string = 'premierrange-local.com';
let str:string = `http://${domain}`;
fixture(`Getting Started`)
.page(str);
test('Test', async t => {
})
这编译并运行良好。因此,page
如果正在传递变量,似乎需要括号括起来的参数,但如果是字符串文字,则不需要?但我的第一次尝试只是引入了一个变量插值,它被视为编译错误。
的定义page()
看起来像这样(来自node_modules/testcafe/ts-defs/index.d.ts
):
page(url: string | TemplateStringsArray): this;
有人可以解释为什么 Typescript 认为我的第一次修改是非法的吗?