1

So i try to overload this method but TypeScript keeps calling its a duplicate method. What obviously is but i want to overload it.

    import {Injectable, EventEmitter} from '@angular/core';

@Injectable()
export class NotificationService extends EventEmitter<any>{
    /**
     *
     */
    constructor() {
        super(false);

    }

    public error(message:string ):void{
        this.emit({message: message, type: 'error'});       
    }

    public error(message:string, type:string):void {
        this.emit({message: message, type: type});
    }

    public success(message:string):void{
        this.emit({message: message, type: 'success'})
    }


}

i tried some other thing to like and it still gave the same error.

  import {Injectable, EventEmitter} from '@angular/core';

@Injectable()
export class NotificationService extends EventEmitter<any>{
    /**
     *
     */
    constructor() {
        super(false);

    }

    public error(message:string, type?:string ):void{
        this.emit({message: message, type: 'error'});       
    }

    public error(message:string, type:string):void {
        this.emit({message: message, type: type});
    }

    public success(message:string):void{
        this.emit({message: message, type: 'success'})
    }


}

it needs to emit a http api response code.

Elty
  • 72
  • 1
  • 13
  • Possible duplicate of [TypeScript function overloading](http://stackoverflow.com/questions/13212625/typescript-function-overloading) – Dinistro Apr 11 '17 at 09:07
  • 1
    Overloading with `TypeScript` isn't possible. If you compile this to JS, their function signature is the same. – Dinistro Apr 11 '17 at 09:08

3 Answers3

1

Typescript and the javascript world in general doesn't support overloading. To achieve what you want, you have to create an error method handling optional parameters.

The ecma6 and typescript added the class keyword but the javascript paradigm didn't change. Javascript's based upon prototype which is basically a big key value array. If you create two methods with the same name, the second will override the first one.

Polochon
  • 2,199
  • 13
  • 13
1

So what i did now is most likely working

import {Injectable, EventEmitter} from '@angular/core';

@Injectable()
export class NotificationService extends EventEmitter<any>{
    /**
     *
     */
    constructor() {
        super(false);

    }

    public error(message:string, type?:string):void {
        this.emit({message: message, type: type?type:'error'});
    }

    public success(message:string):void{
        this.emit({message: message, type: 'success'})
    }


}
n00dl3
  • 21,213
  • 7
  • 66
  • 76
Elty
  • 72
  • 1
  • 13
  • 1
    the two first `error` declarations are at-best useless. – n00dl3 Apr 11 '17 at 09:17
  • Can you explain why ? – Elty Apr 11 '17 at 09:22
  • 1
    because `type?:string` makes the `type` parameter optional, declaring 2 methods would make sense in the java world where there is no optional parameter, but writing `public error(message:string, type?:string):void` is the same as declaring the two previous methods. – n00dl3 Apr 11 '17 at 09:25
  • I'm not even sure declaring a method with the same name several times is valid in typescript. – n00dl3 Apr 11 '17 at 09:28
  • no it's not valid, thats why i didn't find get it ... put thanks to you guys i got it fixed !! Thanks !! – Elty Apr 11 '17 at 09:33
  • I edited you answer to reflect the solution, please mark as accepted, so people asking the same thing, will find a working solution easily. – n00dl3 Apr 11 '17 at 09:35
1

There is no overloading in javascript, typescript being a javascript superset, you won't have overloading with typescript either. With Javascript/typescript object's method are identified by there names.

Actually, overloading is useless with typescript as you can define optional parameters and default values. Writing this :

public error(message:string, type?:string):void {
    this.emit({message: message, type: type?type:'error'});
}

would be an equivalent of overloading the error method (if overloading was allowed), one definition with the type parameter, an other one without it.

Also note that instead of using the ternary operator (foo?foo:bar), you could set a default value for type, instead of just making it optional. Last point, you don't have to write {foo:foo} in an object if the variable that define the value has the same name as the property ({foo} is enough in typescript and es6) :

public error(message:string, type:string="error"):void {
    this.emit({message, type});
}
n00dl3
  • 21,213
  • 7
  • 66
  • 76