Skip to content

Type predicate callbacks #47249

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

Closed
5 tasks done
roobscoob opened this issue Dec 25, 2021 · 3 comments
Closed
5 tasks done

Type predicate callbacks #47249

roobscoob opened this issue Dec 25, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@roobscoob
Copy link

Suggestion

πŸ” Search Terms

"type predicate"

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

The following code does not function, though it can be expected to.

class ExtendedType {
    isExtended: this is ExtendedType { return true }
}

class BaseType {
    isExtended: this is ExtendedType { return false }
}

declare const array: BaseType[]

function takesCb<T extends BaseType>(cb: (arg: BaseType) => arg is T): T {
    for (let item of array) {
        if (cb(item)) return item;
    }
}

const extended = 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):

const packet = 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.

@roobscoob
Copy link
Author

To go into more detail here (re-reading, i noticed it was missed by accident) the core issue here is that in this code sample:

let cb = (t) => t.isXyz()

cb does not share the return type of t.isXyz (that is, this is Xyz) but rather boolean, which I feel is unexpected.

@MartinJohns
Copy link
Contributor

Duplicate of #38390.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 5, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants