I need help in figuring out how to properly assert a type in typescript.
The code that I'm working with is confusing rn (I think I made it more complicated than it needed to be lol :3) so I will just give you the overview of what I'm trying to accomplish.
So basically I have a ConcreteFactory class that implements the IAbstractFactory interface. The ConcreteFactory class can create/produce 3 different Concrete Classes BaseDocument, DocumentA, and DocumentB.
interface IAbstractFactory{
createBaseDocument(): Promise<IAbstractDocument>;
createDocumentA(): Promise<IAbstractDocument>;
createDocumentB(): Promise<IAbstractDocument>;
}
class ConcreteFactory implements IAbstractFactory{
public async createBaseDocument(): Promise<IAbstractDocument> {
return new BaseDocument();
}
public async createDocumentA(): Promise<IAbstractDocument> {
return new DocumentA();
}
public async createDocumentB(): Promise<IAbstractDocument> {
return new DocumentB();
}
}
Note that only the Document class implements the IAbstractDocument interface. DocumentA and DocumentB both extend BaseDocument.
interface IAbstractDocument{
methodBase(){};
}
class BaseDocument implements IAbstractDocument{
methodBase(){};
}
// uses all of BaseDocuments functions but has some extra functions that DocumentB does not have
class DocumentA extends BaseDocument{
methodBase(){};
methodA(){}; // not implemented in IAbstractDocument, throws error
}
// uses all of BaseDocuments functions but has some extra functions that DocumentA does not have
class DocumentB extends BaseDocument{
methodBase(){};
methodB(){}; // not implemented in IAbstractDocument, throws error
}
I also have a class called Tree which will build a Tree, with each node being of type IAbstractDocument
class Tree{
rootNode:IAbstractDocument; // Realistically of type DocumentA or DocumentB, IAbstractDocument is throwing error, explained below
nodes: IAbstractDocument[]; // Realistically of type DocumentA or DocumentB, IAbstractDocument is throwing error, explained below
// this function will create factory and the factory will create DocumentA or Document B
createDocument(){}
addDocument()
}
My goal is to create a Tree of Documents. The rules are DocumentA can contain children nodes, both of type DocumentA and DocumentB, but DocumentB cannot have any children (they can only be leaf nodes).
The problem is:
Although DocumentA and DocumentB both extend BaseDocument. In the Tree class they are defined under the type IAbstractDocument (I did this because I thought that's what the ConcreteFactory is returning). So now I getting an error that the unique functions in DocumentA and DocumentB(methodA and methodB, respectively) are not defined in IAbstractDocument. So I cannot use them in the Tree class, I can only use methodBase().
My question is:
How do I treat both DocumentA and DocumentB as the same yet different Type? They should both be considered Document Nodes in Tree, but they have different functions related to them.
Any help would be appreciated, thank you!
Aucun commentaire:
Enregistrer un commentaire