export interface IAgentList {[key: string]: IAgentBase<any>}
export interface IAgents extends IAgentList {
items?: IItemsParser;
page?: IPageParser;
parser?: IWebParser;
}
export interface IAgentBase<T> {
parse(): Promise<T>;
}
export interface IItemsParser extends IAgentBase<IItem[]> {
}
export interface IPageParser extends IAgentBase<IDataSet[]> {
}
export interface IWebParser extends IAgentBase<any> {
}
export interface IAgentsResult<T> {
items?: IItem[];
page?: IDataSet[];
parser?: any;
}
// в реальности все иначе, написал для понимания желаемого результата
async function runAgents<T extends IAgentList>(agents: IAgentList): Promise<IAgentsResult<T>> {
const result: IAgentsResult<T> = {};
Object.keys(agents).forEach(
async key => result[key] = await agents[key].parse()
);
return result;
}
const agents : IAgents = {...}
const result = runAgents(agents)
async function runAgents<T extends IAgentList>(agents: T): Promise<ResolveResult<T>>
type ResolveResult<T extends IAgentList> = {
[key in keyof T]: T[key] extends IAgentBase<infer R> ? R : never;
}
type IItem = {type: 'IItem'};
type IDataSet = {type: 'IDataSet'};
export type IAgentList = {
[key: string]: IAgentBase<any>;
}
export interface IAgents extends IAgentList {
items: IItemsParser;
page: IPageParser;
parser: IWebParser;
}
export interface IAgentBase<T> {
parse(): Promise<T>;
}
export interface IItemsParser extends IAgentBase<IItem[]> {
}
export interface IPageParser extends IAgentBase<IDataSet[]> {
}
export interface IWebParser extends IAgentBase<any> {
}
export interface IAgentsResult<T> {
items?: IItem[];
page?: IDataSet[];
parser?: any;
}
type ResolveResult<T extends IAgentList> = {
[key in keyof T]: T[key] extends IAgentBase<infer R> ? R : never;
}
// в реальности все иначе, написал для понимания желаемого результата
async function runAgents<T extends IAgentList>(agents: T): Promise<ResolveResult<T>> {
const result: ResolveResult<T> = {} as any;
Object.keys(agents).forEach(
async key => result[key as keyof T] = await agents[key as keyof T].parse()
);
return result;
}
async function main() {
const agents : IAgents = {} as any;
const result = await runAgents(agents)
result // typed
}