【发布时间】:2020-08-21 05:45:20
【问题描述】:
我正在尝试输入一个使用标记模板文字的玩笑test.each
test.each`
height | text
${10} | ${undefined}
${20} | ${undefined}
${10} | ${'text'}
${20} | ${'text'}
`('$height and $text work as expected', ({ height, text }) => {
//...
});
-
height是一个数字 -
text是字符串或未定义
我当然可以将我的类型添加到测试的函数参数中:
({ height, text }: { height: number, text: string? }) => {
//...
});
但这不是我想要的。 test.each 接受泛型类型
test.each<MyType>`
height | text
// ...
`('$height and $text work as expected', ({ height, text }) => {
//...
});
我想知道如何使用该类型来推断函数参数中的类型。
【问题讨论】:
-
test.each<{ height: number, text: string? }>? -
已经试过了,还是不行
-
你能扩展一下“不起作用”吗?给你尝试过的minimal reproducible example。
标签: typescript jestjs