问题
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
回答1:
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:
type RemoveFirstArg<TCtor extends new (... args: any[]) => any > = ReplaceInstanceType<TCtor, { [P in keyof InstanceType<TCtor>]: RemoveArg<InstanceType<TCtor>[P]> }>
function createNewClass<TCtor extends new (... args: any[]) => any >(aCtor: TCtor) : RemoveFirstArg<TCtor>{
// load all of the A method and remove first params
return null as any;
}
type IsValidArg<T> = T extends object ? keyof T extends never ? false : true : true;
type RemoveArg<T> = T extends (a: infer A, b: infer B, c: infer C, d: infer D, e: infer E, f: infer F, g: infer G, h: infer H, i: infer I, j: infer J) => infer R ? (
IsValidArg<J> extends true ? (b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J) => R :
IsValidArg<I> extends true ? (b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I) => R :
IsValidArg<H> extends true ? (b: B, c: C, d: D, e: E, f: F, g: G, h: H) => R :
IsValidArg<G> extends true ? (b: B, c: C, d: D, e: E, f: F, g: G) => R :
IsValidArg<F> extends true ? (b: B, c: C, d: D, e: E, f: F) => R :
IsValidArg<E> extends true ? (b: B, c: C, d: D, e: E) => R :
IsValidArg<D> extends true ? (b: B, c: C, d: D) => R :
IsValidArg<C> extends true ? (b: B, c: C) => R :
IsValidArg<B> extends true ? (b: B) => R :
IsValidArg<A> extends true ? () => R :
T
) : never
type ReplaceInstanceType<T, TNewReturn> = T extends new (a: infer A, b: infer B, c: infer C, d: infer D, e: infer E, f: infer F, g: infer G, h: infer H, i: infer I, j: infer J) => infer R ? (
IsValidArg<J> extends true ? new (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J) => TNewReturn :
IsValidArg<I> extends true ? new (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I) => TNewReturn :
IsValidArg<H> extends true ? new (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H) => TNewReturn :
IsValidArg<G> extends true ? new (a: A, b: B, c: C, d: D, e: E, f: F, g: G) => TNewReturn :
IsValidArg<F> extends true ? new (a: A, b: B, c: C, d: D, e: E, f: F) => TNewReturn :
IsValidArg<E> extends true ? new (a: A, b: B, c: C, d: D, e: E) => TNewReturn :
IsValidArg<D> extends true ? new (a: A, b: B, c: C, d: D) => TNewReturn :
IsValidArg<C> extends true ? new (a: A, b: B, c: C) => TNewReturn :
IsValidArg<B> extends true ? new (a: A, b: B) => TNewReturn :
IsValidArg<A> extends true ? new (a: A) => TNewReturn :
new () => TNewReturn
) : never
//Usage
class A {
public aCall(a: any, payload: string) { }
public bCall(a: any, payload: number) { }
}
// Extending a class
class C extends createNewClass(A) { }
new C().aCall('xxx')
//For interfaces we can just use the type
interface IC extends RemoveFirstArg<typeof A> { }
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
and ReplaceInstanceType
:
type IsValidArg<T> = T extends object ? keyof T extends never ? false : true : true;
type ArgumentTypes<T> = T extends (... args: infer U ) => any ? U: never;
type ReplaceInstanceType<T, TNewInstance> = T extends new (...args: any[])=> infer R ? new (...a: ArgumentTypes<T>) => TNewInstance : never;
type ArgumentTypesSkipOne<T> = T extends (a: any, ... args: infer U ) => any ? U: never;
type RemoveArg<T> = T extends (a: any, ...args: any[])=> infer R ? (...a: ArgumentTypesSkipOne<T>) => R : T;
type RemoveFirstArg<TCtor extends new (... args: any[]) => any > = ReplaceInstanceType<TCtor, { [P in keyof InstanceType<TCtor>]: RemoveArg<InstanceType<TCtor>[P]> }>
function createNewClass<TCtor extends new (... args: any[]) => any >(aCtor: TCtor) : RemoveFirstArg<TCtor>{
// load all of the A method and remove first params
return null as any;
}
Not only is this shorter but it solves a number of problems
- Optional parameters remain optional
- Argument names are preserved
- Works for any number of arguments
回答2:
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:
type RemoveFirstArgOfTwoArgMethods<T> = { [K in keyof T]:
T[K] extends (a: any, payload: infer P) => infer R ? (payload: P) => R : T[K];
}
function createNewClass<T>(aCtor: new (...args: any[]) => T): new (...args: any[]) => RemoveFirstArgOfTwoArgMethods<T> {
const B = (class extends (aCtor as any) {}) as new (...args: any[]) => RemoveFirstArgOfTwoArgMethods<T>;
// you will need to actually decide what that first argument will be
const firstVal: any = "whoKnows";
Object.keys(aCtor.prototype).forEach(k => {
const protoVal = (aCtor.prototype)[k];
if ((typeof protoVal === 'function') && (protoVal.length === 2)) {
B.prototype[k] = function (...args: any[]) { return (protoVal as Function).call(this, firstVal, ...args) }
}
})
return B;
}
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:
class A {
public aCall(a: any, payload: string) {
console.log("aCall(" + a + "," + payload + ")");
}
}
const a = new A();
a.aCall("explicit", "call"); // aCall(explicit, call);
const C = createNewClass(A);
const c = new C();
c.aCall("implicit"); // aCall(whoKnows, implicit);
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!
来源:https://stackoverflow.com/questions/50102620/typescript-new-class-by-evaluate-params