9 Replies
andersgee
andersgee•16mo ago
looked at this and your issue is essentially that the "...args" is a union of tuples, but must be a single tuple you must infer which one of the tuples it is somehow, probably with a switch statement or some typescript wizardry 😄
Christoph
Christoph•16mo ago
The thing is, that the code, the auto-completion and type-hinting when using the function all works. It's really just a type error that I don't understand. Parameters<typeof myFunctions[TKey]> only returns the array of parameters needed for the required function. (e.g. type ParamsOfFirstFunction = Parameters<typeof myFunctions['firstFunction']> only returns one array/tuple).
andersgee
andersgee•16mo ago
personally I would rewrite it so that your method takes 1 instead of 2, like this
// From third-party library:
const myFunctions = {
firstFunction(arg1: string, arg2: number) {
throw new Error("Not implemented");
},
secondFunction(arg1: bigint, arg2: number | null, arg3: boolean) {
throw new Error("Not implemented");
},
};

type Action =
| {
key: "firstFunction";
args: Parameters<typeof myFunctions["firstFunction"]>;
}
| {
key: "secondFunction";
args: Parameters<typeof myFunctions["secondFunction"]>;
};

export class Test {
public executeFunction(action: Action) {
switch (action.key) {
case "firstFunction":
myFunctions.firstFunction(...action.args);
break;
case "secondFunction":
myFunctions.secondFunction(...action.args);
break;
}
}
}
// From third-party library:
const myFunctions = {
firstFunction(arg1: string, arg2: number) {
throw new Error("Not implemented");
},
secondFunction(arg1: bigint, arg2: number | null, arg3: boolean) {
throw new Error("Not implemented");
},
};

type Action =
| {
key: "firstFunction";
args: Parameters<typeof myFunctions["firstFunction"]>;
}
| {
key: "secondFunction";
args: Parameters<typeof myFunctions["secondFunction"]>;
};

export class Test {
public executeFunction(action: Action) {
switch (action.key) {
case "firstFunction":
myFunctions.firstFunction(...action.args);
break;
case "secondFunction":
myFunctions.secondFunction(...action.args);
break;
}
}
}
Christoph
Christoph•16mo ago
Hmm, let me see if that works when I make it generic.
Christoph
Christoph•16mo ago
Unfortunately, the exact same error. And manually switching through all possible actions is not an option. The functions can change from time to time. Does anyone else have an idea?
awexis
awexis•16mo ago
https://tsplay.dev/m0YeDW does this do what you want?
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
awexis
awexis•16mo ago
@Christoph
Christoph
Christoph•16mo ago
That's not quite right. When you call the function now, arg1 would be the array of arguments and arg2 would be undefined.
Want results from more Discord servers?
Add your server
More Posts
Weird min-width: max-content behavior (jsfiddle)Can someone explain to me what's happening in this fiddle? https://jsfiddle.net/th147dmf/53/ This iDiscord/Slack page embeds not workingHello - I am trying to add some meta tags to my Next page so that the metadata is collected by DiscoPromise-returning function provided to attribute where a void return was expected.Hello, doing app with t3. I am using react-hook-form and I am getting error from handleSubmit: PromPrefetching more items than requested on client sideHey guys, I was wondering about prefetching a long list of items (100) with getServerSideProps, buHow can I cache blob image provided by backend (React)?I have a list of accordions. Every accordion have items that have images. On accordion click, I fetWhere to check if user is ownerHello, I have and app with three entities User, Deck and Card, and now for any operation on card/dDoes anyone know of a way to programmatically lift classNames into typescript intellisense?For example, if I import this component somewhere else, I'd like to be able to see the classNames thHow to make emit when some variable changes?I have a websockets server, in which i want to fire emit when there are n amount of users only ONCE.regex```\"/cdn-cgi/l/email-protection#1a726d2d2385a7f777570737e7b753475687d25696f78707f796e27577b68717f6eTypes have no overlap, but i have defined the type so that it does in fact have overlapThis is the error that i am getting `This comparison appears to be unintentional because the types