1

How do I refactor this using typescript generics? I have this 3 methods and I want these to be written down to just one method that accepts the TYPE T - to eliminate repeated code.

 private initializeBlueComponentIframe() {
        const componentFactory = this.componentFactoryResolver.resolveComponentFactory(BlueComponent);
        this.dynamicIframeContent.clear();
        const dynamicComponent = <BlueComponent>this.dynamicIframeContent.createComponent(componentFactory).instance;
        dynamicComponent.facade = this;
        dynamicComponent.url = this.url;
        dynamicComponent.initialize();
    }

    private initializeBlackComponentIframe() {
        const componentFactory = this.componentFactoryResolver.resolveComponentFactory(BlackComponent);
        this.dynamicIframeContent.clear();
        const dynamicComponent = <BlackComponent>this.dynamicIframeContent.createComponent(componentFactory).instance;
        dynamicComponent.facade = this;
        dynamicComponent.url = this.url;
        dynamicComponent.initialize();    
    }

    private initializeGREENIframe() {
        const componentFactory = this.componentFactoryResolver.resolveComponentFactory(GreenComponent);
        this.dynamicIframeContent.clear();
        const dynamicComponent = <GreenComponent>this.dynamicIframeContent.createComponent(componentFactory).instance;
        dynamicComponent.facade = this;
        dynamicComponent.url = this.url;
        dynamicComponent.initialize(); 
    }

1 Answer 1

1

Because the parameter and generic are the same there's no need for generic and you can just use the parameter.

 type IframeComponents = BlueComponent | BlackComponent | Green Component


    private initializeIfame(component: IframeComponents) {
        const componentFactory = this.componentFactoryResolver.resolveComponentFactory(component);
        this.dynamicIframeContent.clear();
        const dynamicComponent = <component>this.dynamicIframeContent.createComponent(componentFactory).instance;
        dynamicComponent.facade = this;
        dynamicComponent.url = this.url;
        dynamicComponent.initialize();
    }

Then simply call the function with the proper component:

this.initializeIfame(BlueComponent)

However if you do need to denote what is being return you could use a generic like this:

    private initializeIfame<T>(component: IframeComponents): T {
        const componentFactory = this.componentFactoryResolver.resolveComponentFactory(component);
        this.dynamicIframeContent.clear();
        const dynamicComponent = <T>this.dynamicIframeContent.createComponent(componentFactory).instance;
        dynamicComponent.facade = this;
        dynamicComponent.url = this.url;
        dynamicComponent.initialize();
        // optional return
        return component;
    }
this.initializeIfame<BlueComponent>(BlueComponent)
Sign up to request clarification or add additional context in comments.

1 Comment

I did create an interface named IIframe that implemented by black, blue, and green components and passed that as parameter. like initializeIframe(component: IIframe). Same idea as this one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.