Issue
I have this function:
network = (action: boolean): void => {
if (action) {
this.action = action;
this.net = true;
this.netd = true;
} else {
this.action = null;
this.net = false;
this.netd = false;
}
}
Is there a way that I can define in typescript that the action can have a value of boolean OR string?
Solution
Yes. Just use a function
instead of var
:
function network(action:boolean):void;
function network(action:string):void;
function network(action: any): void {
if (action) {
this.action = action;
this.net = true;
this.netd = true;
} else {
this.action = null;
this.net = false;
this.netd = false;
}
}
network(''); //okay
network(true); // okay
network(12); // ERROR!
Its called function overloading and you can do this for member functions as well.
Answered By - basarat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.