您似乎没有最新版本的 jest 类型,请尝试更新软件包@types/jest
到最新版本(它包含类型定义Each https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/index.d.ts#L294界面)。
如果由于某些原因这是不可能的,您可以随时使用称为“扩展”的打字稿功能自行“扩展”类型声明合并 https://www.typescriptlang.org/docs/handbook/declaration-merging.html:
// jest.d.ts file
declare namespace jest {
interface Each {
// Exclusively arrays.
<T extends any[]>(cases: ReadonlyArray<T>): (name: string, fn: (...args: T) => any, timeout?: number) => void;
// Not arrays.
<T>(cases: ReadonlyArray<T>): (name: string, fn: (...args: T[]) => any, timeout?: number) => void;
(cases: ReadonlyArray<ReadonlyArray<any>>): (
name: string,
fn: (...args: any[]) => any,
timeout?: number
) => void;
(strings: TemplateStringsArray, ...placeholders: any[]): (
name: string,
fn: (arg: any) => any,
timeout?: number
) => void;
}
interface Describe {
each: Each
}
}
您可能还需要指定类型根 https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types配置选项,以便打字稿可以选择您的自定义类型
Upd:抱歉,我刚刚注意到您的问题并非没有Each
界面但在类型不正确。
在您的情况下,打字稿似乎无法正确推断类型,因此您可能需要显式指定泛型类型,例如:
type TestTuple = [string | number | boolean, boolean];
describe.each<TestTuple>([
['name', true],
[1, true],
[false, true],
[undefined, true],
['no-such-key', false]
])('when key = %s', (a, b) => {
// do your stuff
});