Departializing in typescript

How can I write a utility to convert a partial or potentially partial type to a non-partial type? I'm trying to do something like
type PotentiallyPartial = Discord.AllowedPartial | Discord.Partialize<Discord.AllowedPartial>;

export async function departialize<T extends PotentiallyPartial,
R extends ReturnType<T["fetch"]>>(thing: T): Promise<R> {
if(thing.partial) {
return thing.fetch();
} else {
return thing as R;
}
}
type PotentiallyPartial = Discord.AllowedPartial | Discord.Partialize<Discord.AllowedPartial>;

export async function departialize<T extends PotentiallyPartial,
R extends ReturnType<T["fetch"]>>(thing: T): Promise<R> {
if(thing.partial) {
return thing.fetch();
} else {
return thing as R;
}
}
6 Replies
d.js docs
d.js docs3y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
jr
jrOP3y ago
Do not all partials have a fetch function?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
jr
jrOP3y ago
Hmm, I'm not sure I see how I can use that to help here Is there any way to get the non-partial type from a potentially partial type? E.g. get Discord.Message from Discord.Message | Discord.PartialMessage
chewie
chewie3y ago
just fetch the message .fetch() on the partial object
jr
jrOP3y ago
I am..... I just can't figure out how to get the types correct Typescript doesn't think fetch exists on T otherwise ReturnType<T["fetch"]> would be the easy solution Ok so worst case I can do this
export async function departialize(thing: Discord.User | Discord.PartialUser): Promise<Discord.User>;
export async function departialize(thing: Discord.GuildMember | Discord.PartialGuildMember): Promise<Discord.GuildMember>;
export async function departialize(thing: Discord.ThreadMember | Discord.PartialThreadMember): Promise<Discord.ThreadMember>;
export async function departialize(thing: Discord.Channel | Discord.PartialChannelData): Promise<Discord.Channel>;
export async function departialize(thing: Discord.Message | Discord.PartialMessage): Promise<Discord.Message>;
export async function departialize(thing: Discord.MessageReaction | Discord.PartialMessageReaction): Promise<Discord.MessageReaction>;
export async function departialize<T extends Discord.AllowedPartial | Discord.Partialize<Discord.AllowedPartial>>(thing: T): Promise<unknown> {
if(thing.partial) {
return await (thing as any).fetch();
} else {
return thing;
}
}
export async function departialize(thing: Discord.User | Discord.PartialUser): Promise<Discord.User>;
export async function departialize(thing: Discord.GuildMember | Discord.PartialGuildMember): Promise<Discord.GuildMember>;
export async function departialize(thing: Discord.ThreadMember | Discord.PartialThreadMember): Promise<Discord.ThreadMember>;
export async function departialize(thing: Discord.Channel | Discord.PartialChannelData): Promise<Discord.Channel>;
export async function departialize(thing: Discord.Message | Discord.PartialMessage): Promise<Discord.Message>;
export async function departialize(thing: Discord.MessageReaction | Discord.PartialMessageReaction): Promise<Discord.MessageReaction>;
export async function departialize<T extends Discord.AllowedPartial | Discord.Partialize<Discord.AllowedPartial>>(thing: T): Promise<unknown> {
if(thing.partial) {
return await (thing as any).fetch();
} else {
return thing;
}
}
But there has got to be a better way to do this I'm going with
type PotentiallyPartial = Discord.User | Discord.PartialUser
| Discord.GuildMember | Discord.PartialGuildMember
| Discord.Message | Discord.PartialMessage
| Discord.MessageReaction | Discord.PartialMessageReaction;
export async function departialize<T extends PotentiallyPartial,
R extends ReturnType<T["fetch"]>>(thing: T): Promise<R> {
if(thing.partial) {
return await thing.fetch() as R;
} else {
return thing as any as R;
}
}
type PotentiallyPartial = Discord.User | Discord.PartialUser
| Discord.GuildMember | Discord.PartialGuildMember
| Discord.Message | Discord.PartialMessage
| Discord.MessageReaction | Discord.PartialMessageReaction;
export async function departialize<T extends PotentiallyPartial,
R extends ReturnType<T["fetch"]>>(thing: T): Promise<R> {
if(thing.partial) {
return await thing.fetch() as R;
} else {
return thing as any as R;
}
}
Posting it here in case it's of use to anyone else
Want results from more Discord servers?
Add your server