#custom filter of Channels with their according Type

44 messages · Page 1 of 1 (latest)

pseudo schooner
#

Hey so i would like a function similar to this:

function filterCh(channels:GuildBasedChannel[], [ChannelType.PrivateThread,ChannelType.PublicThread]:ChannelTypes[])

that should return PrivateThreadChannel| PublicThreadChannel

But it doesnt seem to work at all, i also tried with single types and its only working for single objs

main marsh
#

• 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.

pseudo schooner
#

djs 14.7.1
node v18.3.0

smoky citrus
#

i really need test environment

pseudo schooner
#

should i create a sandbox ?

indigo warren
#

I have an idea

#

@pseudo schooner use generics

smoky citrus
#

use what

pseudo schooner
#

i tried... but its doesnt really solve any problem

#

at least from what i thought...

hollow hinge
#

filter narrows the type if you provide it with a typeguarding function, a simple comparison only returns a boolean as you can see

pseudo schooner
#

so how would you get the correct typeguard from the ChannelType enum ?

hollow hinge
#

you have to annotate your arrow funtion: (c): c is ... => c.type === ...

pseudo schooner
#

i do understand but it wouldnt work for a dynamic channeltype and is the same as casting it pretty muhc

#

that actually should give an error, but it doesnt 🤔 same as type casting,

thick fossil
#

You tell it a lie in your type annotations. That’s why it doesn’t error

pseudo schooner
#

yeah i know, so it really isnt a solution to my problem 🔝

thick fossil
#

It sure is, if you stopped lying to TS. Use a generic and pass that generic in the typeguard

pseudo schooner
#

mhm i dont understand how to use a generic here, you have an example

#

passing the channeltype as generic ? ChannelType is an enum tho...

hollow hinge
#

you provide the type to the function in your example, so use a generic parameter for this type

#

you can use the generic in the typeguard e.g. with Extract since Channel is a union type

pseudo schooner
#

i dont provide a type, i provide the ChannelType enum

hollow hinge
#

you have a parameter you assign one of the enum values to

pseudo schooner
#

i cant understand, i m sorry. Can you use the variable name ?

I use an array of enums through which the channel array should return the according type
It does if you do it for a single object, in my example you can call single.ownerId
if you do the same with the array it still returns GuildBasedChannel instead of PublicThreadChannel

#

--

Furthermore i want to provide an array of enums so the enums are dynamic => i cant cast them...

hollow hinge
#

you can Extract different types of channels from Channel with a generic parameter, you can use generics for array parameters

pseudo schooner
#

can you write me an example?

#

i am probably far off from what you want to explain me, sorry!

smoky citrus
#

imo best way even if overwriting

hollow hinge
#

there's a reason ts doesn't allow you to cast the return type of filter to T[]

smoky citrus
#

well yes but

hollow hinge
#

the function returns an array of channels, not channel types

smoky citrus
#

ah that too yeah

#

you cant really do it so it gives type you want to put in

hollow hinge
#

you can

#

refer to the ts playground above

smoky citrus
#

hm wait

#

so basically

#

this was fun