Я получаю следующую ошибку при попытке привязать тип к макету следующим образом:
const mockCommunityStorage: BasicCommunityStorage = mock<
BasicCommunityStorage
>();
const container: Container = new Container();
beforeAll(() => {
container.bind<CommunityResolver>(CommunityResolver).toSelf();
container.bind<CommunityService>(CommunityService).toSelf();
container
.bind<BasicCommunityStorage>(TYPES.BasicCommunityStorage)
.to(mockCommunityStorage);
});
Argument of type 'BasicCommunityStorage' is not assignable to parameter of type 'new (...args: any[]) => BasicCommunityStorage'.
Type 'BasicCommunityStorage' provides no match for the signature 'new (...args: any[]): BasicCommunityStorage'
Мне удалось обойти это, добавив определение конструктора в интерфейс :
new (...args: any[]): BasicCommunityStorage;
Мне интересно, почему я могу связать немодальную реализацию без этой ошибки типа:
@injectable()
export class CommunityStorage implements BasicCommunityStorage {
private store = new Map<string, Community>();
get(id: string): Community | null {
return this.store.get(id) || null;
}
create(
name: string,
ownerUsername: string,
leasingTeamName: string,
companyName: string
): Community {
const community = new Community();
community.id = Date.now().toString();
community.name = name;
community.ownerUsername = ownerUsername;
community.leasingTeamName = leasingTeamName;
community.companyName = companyName;
this.store.set(community.id, community);
return community;
}
}
РЕДАКТИРОВАТЬ: я смог заставить это работать используя шутки:
jest.mock("../store/CommunityStorage");
const mockCommunityStorage = CommunityStorage as jest.Mock<CommunityStorage>;
beforeAll(() => {
decorate(injectable(), mockCommunityStorage);
container.bind<CommunityResolver>(CommunityResolver).toSelf();
container.bind<CommunityService>(CommunityService).toSelf();
container
.bind<BasicCommunityStorage>(TYPES.BasicCommunityStorage)
.to(mockCommunityStorage);
communityService = container.get<CommunityService>(CommunityService);
});