Are union types supported in args.pick?

I really wanted to know whether i could do union types in arguments picking, coming from discord-akairo it had that thing where you can have multiple types which then gets resolved at argument prompting
3 Replies
Lioness100
Lioness10015mo ago
You should do:
const union = await args.pick('firstType').catch(() => args.pick('secondType'));
const union = await args.pick('firstType').catch(() => args.pick('secondType'));
Xeno™
Xeno™OP15mo ago
pepeCry that's not very clean though if i wanted multiple unions i'll have to make a chain
Lioness100
Lioness10015mo ago
You could always make a utility function
type ArgMethods = {
[K in keyof Args]: Args[K] extends Function ? K : never;
}[keyof Args];

const getUnionArg = async <T>(args: ArgMethods, method: ArgMethods<T>, ...opts: IArgument<T>[]) => {
let lastError: unknown;
for (const opt of opts) {
try {
return await args[method](opt);
} catch (error) {
lastError = error;
}
}

throw lastError;
};
type ArgMethods = {
[K in keyof Args]: Args[K] extends Function ? K : never;
}[keyof Args];

const getUnionArg = async <T>(args: ArgMethods, method: ArgMethods<T>, ...opts: IArgument<T>[]) => {
let lastError: unknown;
for (const opt of opts) {
try {
return await args[method](opt);
} catch (error) {
lastError = error;
}
}

throw lastError;
};
const union = await getUnionType(args, 'pick', 'firstType', 'secondType');
const union = await getUnionType(args, 'pick', 'firstType', 'secondType');
That's probably not completely valid code but something like that Or
const getUnionArg = async <K extends keyof Argtype, T>(cb: (opt: K) => Promise<T>, ...opts: K[]) => {
let lastError: unknown;
for (const opt of opts) {
try {
return await cb(opt);
} catch (error) {
lastError = error;
}
}

throw lastError;
}
const getUnionArg = async <K extends keyof Argtype, T>(cb: (opt: K) => Promise<T>, ...opts: K[]) => {
let lastError: unknown;
for (const opt of opts) {
try {
return await cb(opt);
} catch (error) {
lastError = error;
}
}

throw lastError;
}
const union = await getUnionType((opt) => args.pick(opt), 'firstType', 'secondType');
const union = await getUnionType((opt) => args.pick(opt), 'firstType', 'secondType');
Want results from more Discord servers?
Add your server