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(15048): One-sided Type Predicates #52255

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

graphemecluster
Copy link
Contributor

Fixes #15048

Why not nominal type (a.k.a. type brand/tag)? It is excessive, and to avoid the trouble people would instead write something like ((v): v is number => Number.isInteger(v))(v) && (() => true)(), given that it just works. It looks bad when hovering over the variables too, especially if there are multiple layers of type guards.
Particularly for the built-in lib, we don't want something like isInteger(number: unknown): number is number & { readonly integer: unique symbol };, so this is the feasible solution.

Ultimately I would also like to implement negative predicate and predicate with non-boolean return value such that an overload for {ReadonlyArray, Array}.some like some<S extends T>(predicate: (value: T, index: number, array: T[]) => !(value is subtypeof S if truthy), thisArg?: any): !(this is subtypeof S[]); could be added to the built-in lib, which addresses #46650 and #47574.

The exact syntax is not confirmed, and is open to discussion.

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jan 15, 2023
@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #15048. If you can get it accepted, this PR will have a better chance of being reviewed.

@sandersn sandersn self-assigned this Feb 1, 2023
@sandersn
Copy link
Member

sandersn commented Feb 1, 2023

Can you mark this as draft or wait until we agree on a design in #15048 ? There's not much point in the reviewing the code when the semantics are still up in the air.

@graphemecluster
Copy link
Contributor Author

Definitely will wait for it.

@sandersn sandersn marked this pull request as draft March 10, 2023 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggestion: one-sided or fine-grained type guards
3 participants