Search code examples
angulartypescriptvariable-types

What would be a type of an event in Angular 2?


I'm trying to specify the types of all my methods/parameters so instead of:

someCoolFunction(param) { ... }

I try to use the type safe'ish version like this:

someCoolFunction(param: number): void { ... }

When I bounce around types that I've declared myself, it's easy. When it comes to types brought from other packages, I also often know what they are as I explicitly specify my imports.

However, I'm not sure what type to give to the event, to the object and to the output in the method below.

someBoundFunction(event, junk) {
  ...
  return {a: 1, b: 2};
}

I've seen any being used but I'm not sure if it's applicable in this case. Also, I'm sensing that it's just incorrect for the event call. (If it matters, the call binding to the function can look as below.)

<custom-thing (output)="someBoundFunction($event, {x:"x",y:"y"})">!</custom-thing>

Solution

  • I may be misunderstanding your question, but in this case the type of $event can vary. $event is just a proxy for whatever is transmitted via the emit method in custom-thing.

    For example, if I had an output on my component called output.

    @Output() output: EventEmitter<number> = new EventEmitter<number>();
    
    emitEvent() {
       this.output.emit(5); // $event is 5 of type number
    }