I know that function overloading is not supported in typescript and javascript.
And I'm studying a detour method to work like this function overloading.
My case is as follows.
first:
The last ans first argument is fixed.
public A(arg1:number, arg2:number, argLast:any){
}
public A(arg1:number, arg2:number, arg3:Array<any>, argLast:any){
}
Second :
There is an indicator for whether the function is overloaded.
Of course, as in the above example, it is possible, but I have to create it through a new interface, so it does not fit in my case.
I have tried various function overloading methods.
I also implemented it through john resig blog.
(https://johnresig.com/blog/javascript-method-overloading/)
The code below is an example I made through the above blog.
function addMethod (object, name, fn) {
var old = object[ name ]
object[ name ] = function () {
if (fn.length === arguments.length) {
return fn.apply(this, arguments)
} else if (typeof old === 'function') {
return old.apply(this, arguments)
}
}
}
export class Users{
find(...arg: any[]){
Users.prototype['findOVF'](arg)
}
}
addMethod(Users.prototype, 'findOVF', function () {
console.log('ARG 0')
})
addMethod(Users.prototype, 'findOVF', function (name:string, age:number) {
console.log('ARG 2')
})
addMethod(Users.prototype, 'findOVF', function (first_name:string, last_name:string,age:number) {
console.log('ARG 3')
})
var users = new Users()
users.find()
users.find('John',19)
users.find('John', 'Resig', 19)
When I use this method, I call the function with the parameter (... arg).
I've tried to implement all the methods of the blog below.
(https://blog.mariusschulz.com/2016/08/18/function-overloads-in-typescript)
public A(arg1:number, arg2:number, argLast:any){
}
public A(arg1:number, arg2:number, arg3:Array<any>|null, argLast:any){
}
If I implement function overloading this way, I get an error every time I call the A function. It's not really function overloading.
But I have not found a way to implement these two elements at the same time.
I want to know if this is impossible with the typescript grammar or if there is any other possibility.
Thanks for seeing my awkward English.
