Skip to content
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

interaction type narrowing left with never type #7160

Closed
yi-fan-song opened this issue Dec 29, 2021 · 2 comments · Fixed by #7164
Closed

interaction type narrowing left with never type #7160

yi-fan-song opened this issue Dec 29, 2021 · 2 comments · Fixed by #7164

Comments

@yi-fan-song
Copy link

Issue description

When handling an interaction, if I narrow the type using Interaction.inGuild(), the type is narrowed to one such that further calls to Interaction.inRawGuild() or Interaction.inCachedGuild() will end up with the type never.
My naive fix suggestion is to simply change the return type of Interaction.inGuild() from this is Interaction<'present'> to this is Interaction<'cached' | 'raw'>. This would fix my issue, but I don't know what the implications of this change are.
Hopefully I haven't misunderstood that inGuild() and inCachedGuild() are not mutually exclusive.

As a side note: 'present' is a bit weird of a name, I'm guessing that it means that it exists?

Code sample

import { CommandInteraction } from "discord.js";

const handleAsync = async (i: CommandInteraction) => {
    if (!i.inGuild()) {
        return;
    }
    // i is now CommandInteraction<"present"> & CommandInteraction<CacheType>

    if (i.inRawGuild()) {
        // i is never
        return;
    }

    if (i.inCachedGuild()) {
        // i is never
        return;
    }
}

discord.js version

13.4.0

Node.js version

16.13.1

Operating system

WSL 2.0 on Windows 11

Priority this issue should have

Low (slightly annoying)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

GUILDS

I have tested this issue on a development release

No response

@DTrombett
Copy link
Contributor

DTrombett commented Dec 29, 2021

Actually, the only solution would be to use this is Interaction<'cached' | 'raw'> but this seems to bring another issue: guildId is Snowflake | null instead of Snowflake when using inGuild()

image
image

@yi-fan-song
Copy link
Author

between djs 13.3.1 and 13.4.0, the type for CacheTypeReducer went from

export type CacheTypeReducer<
  State extends CacheType,
  CachedType,
  RawType = CachedType,
  PresentType = CachedType | RawType,
  Fallback = PresentType | null,
> = State extends 'cached'
  ? CachedType
  : State extends 'raw'
  ? RawType
  : State extends 'present'
  ? PresentType
  : Fallback;

to

export type CacheTypeReducer<
  State extends CacheType,
  CachedType,
  RawType = CachedType,
  PresentType = CachedType | RawType,
  Fallback = PresentType | null,
> = [State] extends ['cached']
  ? CachedType
  : [State] extends ['raw']
  ? RawType
  : [State] extends ['present']
  ? PresentType
  : Fallback;

Reverting that fixes the Snowflake | null for me. But obviously that would also reintroduce the bug that it originally fixed. I would need a little more insight into this 'present' cache type. If it can be safely be represented by 'cache' | 'raw', then that will also work, this is definitely not easy to change without breaking types for anyone using CacheTypeReducer in their code.

@iCrawl iCrawl assigned iCrawl and suneettipirneni and unassigned iCrawl Dec 29, 2021
@iCrawl iCrawl added typings and removed need repro labels Dec 29, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants