我正在尝试输入一个test.each使用标记模板文字的笑话
test.each`
height | text
${10} | ${undefined}
${20} | ${undefined}
${10} | ${'text'}
${20} | ${'text'}
`('$height and $text work as expected', ({ height, text }) => {
//...
});
Run Code Online (Sandbox Code Playgroud)
height是一个数字text是一个字符串或未定义当然,我可以将我的类型添加到测试的函数参数中:
({ height, text }: { height: number, text: string? }) => {
//...
});
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的。test.each接受泛型类型
({ height, text }: { height: number, text: string? }) => {
//...
});
Run Code Online (Sandbox Code Playgroud)
我想知道如何使用该类型来推断函数参数中的类型。
我遇到这个问题是因为我正在寻找相同的解决方案,但我得出的结论是这是不可能的,因为each当传递的参数不是模板文字形式时,函数的类型仅使用泛型。当您传入表时只能使用泛型类型。
所以你的例子看起来像这样:
test.each<{ height: number; text: string | undefined }>([
{ height: 10, text: undefined },
{ height: 20, text: undefined },
{ height: 10, text: 'text' },
{ height: 20, text: 'text' },
])('$height and $text work as expected', ({ height, text }) => {
//...
});
Run Code Online (Sandbox Code Playgroud)
这是类型文件的链接。 @types/jest 各个接口
| 归档时间: |
|
| 查看次数: |
6007 次 |
| 最近记录: |