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

feat(Channel): add isText() type guard #4745

Merged
merged 3 commits into from Aug 31, 2020
Merged

feat(Channel): add isText() type guard #4745

merged 3 commits into from Aug 31, 2020

Conversation

vaporoxx
Copy link
Contributor

Please describe the changes this PR makes and why it should be merged:

Currently, there is no easy way in TypeScript to find out if a channel is text-based or not.

['text', 'dm', 'news'].includes(channel.type)
channel.type === 'text' || channel.type === 'dm' || channel.type === 'news'

Both of these examples work fine during runtime, but don't act as a type guard; If channel had the type Channel before the check, it will also keep it afterwards.

channel instanceof TextChannel || channel instanceof DMChannel || channel instanceof NewsChannel
channel as TextChannel

These two would actually narrow the type down to only text-based channels, but both aren't really optimal. The former option is too long and the latter doesn't actually check the type, it just tells the compiler that it will be a TextChannel even if it may not be one.

I generally think that checks like this should be able without having to either import the classes and doing multiple instanceof checks or casting its type which may lead to side effects.

Status

  • Code changes have been tested against the Discord API
  • I know how to update typings and have done so

Semantic versioning classification:

  • This PR changes the library's interface (methods or parameters added)
    • This PR includes breaking changes (methods removed or renamed, parameters moved or removed)
  • This PR only includes non-code changes, like changes to documentation, README, etc.

Copy link
Member

@kyranet kyranet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an alternative that also works:

src/structures/Channel.js Outdated Show resolved Hide resolved
@iCrawl iCrawl requested a review from vladfrangu August 29, 2020 10:10
@advaith1
Copy link
Contributor

I feel like naming this isText is misleading, maybe isTextBased or similar?

@iCrawl
Copy link
Member

iCrawl commented Aug 29, 2020

How so? Channel#isText can mislead to what assumption?

@advaith1
Copy link
Contributor

it sounds like it only checks if it is a text channel, and that it should be false for news channels

@HarmoGlace
Copy link
Contributor

For "normal" people a news channel is still a text channel. But yes, it is exact to say it is a text-based channel

@advaith1
Copy link
Contributor

In the UI it is treated as a seperate channel type, as it is
img

@iCrawl
Copy link
Member

iCrawl commented Aug 29, 2020

Sure, but I don't see how isTextBased is any better than isText.

It's both not explaining that a news channel is included, especially not in/with that UI argument.

@HarmoGlace
Copy link
Contributor

HarmoGlace commented Aug 29, 2020

Also, why is it a method named isText instead of a getter named text ? Because all bolean getters in the library are not named by starting with is

@vaporoxx
Copy link
Contributor Author

Getters count as properties and X is Y is only valid for function return types.

@HarmoGlace
Copy link
Contributor

HarmoGlace commented Aug 29, 2020

I know it is counted as a property but I don't see what you mean, e.g. TextChannel#viewable returns a boolean and it is not named isViewable https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=viewable so I don't see why it would not be named text instead of isText

@vaporoxx
Copy link
Contributor Author

If you have a look at the changes for index.d.ts, you'll see that it's not a regular boolean but instead a type guard so that you can use TextBasedChannel methods on it. That type is only available as function return types so it can't be added as a getter.

@iCrawl iCrawl merged commit b0ab37d into discordjs:master Aug 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants