I'm on TypeScript 3.1, and Brian Terlson (@bterlson) asked if there was a way to construct all constructors inside of an array. However, trying to do this in a way that worked for object properties as well as array-likes seems impossible
typeConstructAll<T>={[KinkeyofT]: InstanceType<T[K]>};typeFoo=ConstructAll<[typeofDate,typeofRegExp]>;This gives an error on the first type argument to InstanceType:
[ts] Type 'T[K]' does not satisfy the constraint 'new (...args: any[]) => any'.
Ugh. Okay, fine.
typeAnyConstructor=new(...args: never[])=>unknown;typeConstructAll<TextendsRecord<string,AnyConstructor>>={[KinkeyofT]: InstanceType<T[K]>};typeFoo=ConstructAll<[typeofDate,typeofRegExp]>;Which gives me an error on the first type argument to ConstructAll:
[ts]Type'[Date, RegExp]'doesnotsatisfytheconstraint'Record<string, new (...args: any[]) => any>'.Indexsignature is missingintype'[Date, RegExp]'.
I'll save you time; the following doesn't work either:
typeAnyConstructor=new(...args: never[])=>unknown;typeConstructAll<TextendsAnyConstructor[]|Record<string,AnyConstructor>>={[KinkeyofT]: InstanceType<T[K]>};typeFoo=ConstructAll<[typeofDate,typeofRegExp]>;You end up with the original scenario. I end up not being able to reuse InstanceType at all because it has a constraint that we can't dive into. I have to instead use some unconstrained conditional type.
typeConstructor<T>=new(...args: any[])=>T;typeConstructAll<TextendsConstructor<unknown>[]|Record<string,Constructor<unknown>>>={[KinkeyofT]: T[K]extendsConstructor<infer T> ? T : never;};typeFoo=ConstructAll<[typeofDate,typeofRegExp]>It seems problematic that we didn't want to "split the world" into array mapped types and object mapped types, but you end up with that problem anyway in some cases.
I'm on TypeScript 3.1, and Brian Terlson (@bterlson) asked if there was a way to construct all constructors inside of an array. However, trying to do this in a way that worked for object properties as well as array-likes seems impossible
This gives an error on the first type argument to
InstanceType:Ugh. Okay, fine.
Which gives me an error on the first type argument to
ConstructAll:I'll save you time; the following doesn't work either:
You end up with the original scenario. I end up not being able to reuse
InstanceTypeat all because it has a constraint that we can't dive into. I have to instead use some unconstrained conditional type.It seems problematic that we didn't want to "split the world" into array mapped types and object mapped types, but you end up with that problem anyway in some cases.