【发布时间】:2022-09-23 03:51:53
【问题描述】:
我目前正在将项目转换为 TypeScript。我有这个 Algorithm 对象,它包含一个 getRun 函数和一个 edgesRepresentation 字符串,其中包含有关如何表示边缘的信息(\"adjacencyList\" | \"adjacencyMatrix\" | \"edgeList\",尽管现在只使用\"adjacencyList\")。如果可能的话,我不想让 IAlgorithm 接口成为 edgesRepresentation 的通用接口(因为我认为没有理由仅仅因为它的 run 函数也是一个通用的算法)所以我最好寻找一个更动态的解决方案.问题是,当 IAlgorithm 有一个返回 run 函数的 getRun 函数时,run 函数(我对它进行泛型没有问题)需要对边的表示方式进行假设,但对于不同的 edgesRepresentation 对象,这些假设是不同的。我想要类似的东西:
interface IAlgorithm {
getRun: (arg0: {considers: Considers, setIsDone: (arg0?: boolean)=>void}) => IRunType;
}
export interface IRunType<T extends EdgesRepresentationType> {
(nodesIds: List<string>, edgeList: T):void;
}
type AdjacencyListType = Map<string, Map<string, typeof EdgeRecord>>;
export enum EdgesRepresentationType {
adjacencyList=AdjacencyListType
}
这里的 EdgeRecord 只是一个包含边信息的不可变记录。
像这样的东西也很好:
interface IAlgorithm<T extends EdgesRepresentationType> {
getRun: (arg0: {considers: Considers, setIsDone: (arg0?: boolean)=>void}) => IRunType<T>;
}
export type ITopSort = IAlgorithm<EdgesRepresentationType.adjacencyList>;
export interface IRunType<T extends EdgesRepresentationType> {
(nodesIds: List<string>, edgeList: T):void;
}
type AdjacencyListType = Map<string, Map<string, typeof EdgeRecord>>;
export enum EdgesRepresentationType {
adjacencyList=AdjacencyListType
}
尽管我的 TypeScript 知识相当有限,但我只是找不到解决方法。
-
你展示了两件你想要的东西,而不是你拥有的东西或这两者有什么问题。你能解释一下缺少什么吗?
标签: javascript typescript