I have a a builder function that take an array of objects, create classes from those objects, and returns an object containing those classes. The output of that builder provides proper type hinting for the returned classes. This is based around a 'type reducer' provided in this answer.
However, the ConstructorParameters<T>
utility type is very sensitive to when you get proper type hinting and when you do not. I do not fully understand when I get that hinting and when it simply returns never[]
Currently, I am relying on a sub-builder function to get the proper type hinting for the individual class parameters. The result is something like this...
const Drivers = CreateDrivers([
Driver({ id: 'D1', driver: Driver1, settings: ['', 4] }), //<--- Currently need 'Driver' builder function to properly type check 'settings' property
Driver({ id: 'D2', driver: Driver2, settings: [false, {nested: true}] }), //<--- Can we remove the need for the 'Driver' builder function and bake it into 'CreateDrivers' instead?
Driver({id: 'D5', driver: Driver2, settings: [true, {nested: false}]})
])
Drivers.D1
// typeof 'Driver1' class with full autocomplete
Drivers.D2
// typeof 'Driver2' class with full autocomplete
I would like to not have to rely on a sub-builder function just for the purposes of type-hinting these parameters. I have tried to merge the various types together but to no avail. Functional playground link in my current status is below.