You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following code does not function, though it can be expected to.
classExtendedType{isExtended: this isExtendedType{returntrue}}classBaseType{isExtended: this isExtendedType{returnfalse}}declareconstarray: BaseType[]functiontakesCb<TextendsBaseType>(cb: (arg: BaseType)=>arg is T): T{for(letitemofarray){if(cb(item))returnitem;}}constextended=takesCb(t=>t.isExtended());// extended is expected to be of type ExtendedType
π Motivating Example
I'm building a client for a server, the client emits packets when received.
I have a base ClientBoundPacket class, which other packets extend.
all ClientBoundPackets have methods that look like packet.isKeepAlive() for easily checking and casting their type.
A useful pattern is being able to call an async function and have it resolve when it receives a packet that passes a boolean check.
(e.g. check for a keep alive packet):
constpacket=client.waitForPacket(packet=>packet.isKeepAlive());// packet should be of type ClientBoundKeepAlivePacket (packet.isKeepAlive() returns this is ClientBoundKeepAlivePacket)
π» Use Cases
I wish for this to be implemented to extend the power of an already awesome tool, type predicates.
The text was updated successfully, but these errors were encountered:
Suggestion
π Search Terms
"type predicate"
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
The following code does not function, though it can be expected to.
π Motivating Example
I'm building a client for a server, the client emits packets when received.
I have a base
ClientBoundPacket
class, which other packets extend.all
ClientBoundPacket
s have methods that look likepacket.isKeepAlive()
for easily checking and casting their type.A useful pattern is being able to call an async function and have it resolve when it receives a packet that passes a boolean check.
(e.g. check for a keep alive packet):
π» Use Cases
I wish for this to be implemented to extend the power of an already awesome tool, type predicates.
The text was updated successfully, but these errors were encountered: