-
-
Notifications
You must be signed in to change notification settings - Fork 4k
types(Interaction): allow Interaction
cached properties to be type narrowed
#6668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
types(Interaction): allow Interaction
cached properties to be type narrowed
#6668
Conversation
…ypes/interaction-member-narrowing
Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
…om/suneettipirneni/discord.js into types/interaction-member-narrowing
Co-authored-by: Rodry <38259440+ImRodry@users.noreply.github.com>
Although I like the concept of what this checks I can't help but feel the interface / parameter is really awkward to use in practice. Combining the truthy conditions to achieve the assertions, although confusing, would work from what I can see. However this method returning false doesn't seem to have been considered.
I think combining these into one method with a boolean parameter is where I'm finding it awkward. I feel like |
When this was brought up on the discord server I also thought of it as a separate method, the only issue is I wouldn’t know what to call it but if someone does then I think it would be a preferred scenario |
8e99dff
to
a331029
Compare
…ypes/interaction-member-narrowing
Interaction#member
to be type narrowedInteraction
cached properties to be type narrowed
Substantial changes have been made since this PR originally was opened, rather than just being able to narrow regular // Narrow two levels
if (interaction.isCommand() && interaction.isInCachedGuild()) {
// interaction is now of type GuildCommandInteraction<'cached'>;
...
// Because we are in a cached guild, we can intuitively narrow `Message | APIMessage` to `Message`
const message: Message = await interaction.fetchReply();
} It now can not only infer guild specific properties are of a certain type, but can also infer typings for methods specific to Alternatively you can use the types to give expectations as to what your function recieves: {
...
// The command handler run method only accepts command interactions where the bot is in the same guild.
execute(interaction: GuildCommandInteraction<'cached'>) {
// No need to verify cached or not at this point, the parameter type guarantees it's in a cached guild.
}
} This eliminates redundant type checking/casts in areas where the developer knows the guild will be cached. |
48aaa04
to
e7f7e56
Compare
131f0bd
to
dcad948
Compare
d169f07
to
b4c353a
Compare
b4c353a
to
6ba34b8
Compare
…ypes/interaction-member-narrowing
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
Please describe the changes this PR makes and why it should be merged:
Allows you to narrow out a
GuildMember
or aAPIGuildMember
from theGuildMember | APIGuildMember
union.Alternatively...
Also look at changes here: #6668 (comment)
Some have brought up "Why not use
instanceof interaction.member
"The answer is because it's not as elegant and it doesn't mutate any other types on the interaction such as
guild
and the removal and/or assertion of properties being null. In some circumstances it can be misleading, the methods are more explicit in their purpose.Status and versioning classification: