i7326 i7326 - 1 year ago 214
TypeScript Question

Typescript Error - TS2339 on ComponentRef

I am facing a typescript error

error TS2339: Property 'close' does not exist on type '{}'

Code where i face issue :-


import { Directive, ComponentFactoryResolver, ComponentRef, ViewContainerRef } from '@angular/core';
selector: '[child]'
export class HomeDirective {
constructor(private viewContainer: ViewContainerRef,
private componentFactoryResolver: ComponentFactoryResolver) {
openComp(component:any) : ComponentRef<any> {
let componentFactory =
let componentRef = this.viewContainer.createComponent(componentFactory);
componentRef.instance.close.subscribe(() => {
//do something

return componentRef;



import { Component, EventEmitter } from '@angular/core';
import { HomeService } from '../home.service';
selector: 'child-component',
providers: [HomeService],
template: `<h3>child-component</h3>`
export class ChildComponent {
close = new EventEmitter();
constructor() {
ngOnDestroy() {

and i am invoking openComp()


import { Component ,ViewChild } from '@angular/core';
import { HomeService } from './home.service';
import { HomeDirective } from './home.directive';
import { ChildComponent } from './child/index';
moduleId: module.id,
selector: 'sd-home',
providers: [HomeService],
templateUrl: 'home.component.html',
styleUrls: ['home.component.css'],
entryComponents: [ChildComponent],
export class HomeComponent {
@ViewChild(HomeDirective) homeDirective: HomeDirective;
constructor(private _homeService: HomeService) {
openChild() {

Can anyone help me out? I am a newbie in angular 2 and typescript. my codings might be wrong. please correct me if my codings are wrong.

PS: Even though typescript throws this error, this code works as i wanted(in dev build). but cant do prod build


Answer Source

You can write:



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download