I have this code:
type Constructable = {new(...args: any[]): any, prototype: any}; // Represents a class (that can be called with new)
type WithOnlyType<O, T> = {[P in keyof O as O[P] extends T? P: never]: O[P]}; // https://stackoverflow.com/questions/51419176/how-to-get-a-subset-of-keyof-t-whose-value-tk-are-callable-functions-in-typ
// Override a constructor with name C in a scope S
function extendConstructor<
S,
T extends WithOnlyType<S, Constructable>,
C extends keyof T>
(
className: C, constructor: (...args: ConstructorParameters<T[C]>) => any, scope: S) {
...}
However, ConstructorParameters<T[C]>
is erroring because Type 'T[C]' does not satisfy the constraint 'abstract new (...args: any) => any'.
Which is odd, because T contains only values that fit that requirement (are Constructable
) and C is keyof T
. However, farther down (at the bottom of) the error, I found:
Type 'S[string]' is not assignable to type 'abstract new (...args: any) => any'
This is true, S could have a lot more values than just classes, however, T is a lot more narrowed than S, and C than string. How to I stop tsc from expanding these types?