Elty
Elty

Reputation: 117

overload don't work Angular 2

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.

Upvotes: 2

Views: 1653

Answers (3)

n00dl3
n00dl3

Reputation: 21564

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});
}

Upvotes: 1

Elty
Elty

Reputation: 117

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'})
    }


}

Upvotes: 1

Polochon
Polochon

Reputation: 2219

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.

Upvotes: 1

Related Questions