代码如下:
// file1.ts
interface X { ... } // Don't want to directly export this (private/internal)
// Assume LibInterface<T> is some random interface from an external library
export const func: (arg: LibInterface<X> => string) = ...;
// file2.ts (essentially the jest test file)
import { func } from './file1';
const x: X = ...;
const arg: LibInterface<X> = ...;
expect(func(arg)).toEqual(...);在上面的例子中,我如何引用第二个文件中的X类型?
使用X和jest-mock-extended类型的复杂用例:const mockX = mock<X>(...);
我已经搜索过了,也在https://www.typescriptlang.org/docs/handbook/utility-types.htmlhttps://www.typescriptlang.org/docs/handbook/utility-types.html上找过了,但还是找到了解决方案。
发布于 2020-05-04 23:23:10
您是否能够将泛型类型添加到函数中?应该是这样的:
// file1.ts
interface X { ... } // Don't want to directly export this (private/internal)
// Assume LibInterface<T> is some random interface from an external library
export const func: <T extends X = X>(arg: LibInterface<T> => string) = ...;
// file2.ts (essentially the jest test file)
import { func } from './file1';
const x: X = ...;
const arg: LibInterface<X> = ...;
expect(func(arg)).toEqual(...);https://stackoverflow.com/questions/61595632
复制相似问题