Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Type 'EventEmitter' is not generic" ERROR in angular

I'm currently following a tutorial and the tutorial is making use of EventEmitter. The code goes like this

@Output() ratingClicked: EventEmitter<string> =         new EventEmitter<string>(); 

But it visual studio code gives me these errors:

  1. Type 'EventEmitter' is not generic.
  2. Expected 0 type arguments, but got 1.

Even in the angular website it looks like that code is correct.

I'm currently using Angular CLI: 1.7.4; Node: 8.11.1; Typescript: 2.8.1

like image 832
thegreathypocrite Avatar asked Apr 19 '18 02:04

thegreathypocrite


People also ask

What does EventEmitter do in Angular?

EventEmitterlink. Use in components with the @Output directive to emit custom events synchronously or asynchronously, and register handlers for those events by subscribing to an instance.

How do I declare EventEmitter?

Simply use it to emit events from your component. Take a look a the following example. @Component({ selector : 'child', template : ` <button (click)="sendNotification()">Notify my parent! </button> ` }) class Child { @Output() notifyParent: EventEmitter<any> = new EventEmitter(); sendNotification() { this.

Should I use EventEmitter or subject?

Conclusion. Use Eventemitter when transferring data from child component to parent component. Use Subject to transfer data from one component to another component.

Can EventEmitter return value?

The problem here is that an EventEmitter function can't return a value since it's asynchronous (although from rc2 it seems that this is optional by passing true to the new EventEmitter function? Even doing so won't fix this issue however). So isValid will always be true regardless of what the function returns.


1 Answers

You are probably using the node native EventEmitter from node/index.d.ts i.e.

import { EventEmitter } from 'events'; 

Fix

Change the import to the one from angular:

import { EventEmitter } from '@angular/core'; 
like image 153
basarat Avatar answered Sep 25 '22 02:09

basarat