I'm looking to set the type of T (a generic) equal to the type of the listener field, so that I can have auto completion for "firstParameter" whether the listener is the default value or a custom one. Is this achievable? If not, is there another solution besides setting everything to 'any'?
Currently, I am only able to have one type at a time, either dummy or upper, but not both simultaneously.
Here is the code snippet:
const upper = ({ helloWorld }: { helloWorld: string }) =>
helloWorld.toUpperCase();
const dummy = ({ worldHello }: { worldHello: string }) => worldHello;
type objType<T extends (...args: any[]) => any = typeof upper> = {
listener?: T;
firstParameter: Parameters<T>[0];
};
const obj: objType = {
listener: upper,
firstParameter: {
helloWorld: "hello world", // auto completion
},
};
let arr: objType[] = [
obj,
{ listener: dummy, firstParameter: { worldHello: "world hello" } },
]; // desired outcome
let dummyObj: objType = {
listener: dummy,
firstParameter: {
// I want auto completion here without specifying the type with objType<typeof dummy>
}
}