class A {
public aCall(a: any, payload: string) {}
public bCall(a: any, payload: number) {}
public cCall(a: any) {}
.
.
.
}
function createNewClass(aCtor: A) {
// load all of the A method and remove first params
// generic code on here
// Final result should be like this
return class B {
public aCall(payload: string) {}
public bCall(payload: number) {}
}
}
// C.d.ts
interface C extends createNewClass(A) {}
Can I have a function (or decorator on the method) to evaluate the incoming class and generate new class with removing all first params so that I can use the new class for extending or it just can't to do it
See below for 3.0 answer
You can use a similar approach to this answer. You will need to replace the return type of the constructor, and use a mapped type to create new functions that omit the first argument:
Note The reason for the many, many similar lines is that we need to remap each constructor/function with a specific number of arguments. The implementation above works for 10 arguments, which should be enough but more can be added.
Edit
Since the original question was answered typescript has improved the possible solution to this problem. With the addition of Tuples in rest parameters and spread expressions we now don't need to have all the overloads for
RemoveArg
andReplaceInstanceType
:Not only is this shorter but it solves a number of problems
If, for some reason, you care about actually trying to implement this thing, you could do something like the following. Note that I'm only going to replace methods with two arguments. If you need to do all methods, the typing would have to be more elaborate as in @TitianCernicova-Dragomir's answer:
The idea is that it will extend the original class but replace its two-argument methods with new single-argument methods that call the original method with a constant first argument (in this case it's the string
"whoKnows"
but you might want something else).You can verify that the above works:
There are probably all sorts of caveats when it comes to playing games with classes like this, so be careful that you really understand your use case and what happens when faced with behavior that doesn't conform to it.
Hope that helps. Good luck!