Skip to content

Proposal: Predicate inferer #973

@emmnull

Description

@emmnull

Type description + examples

With typescript now supporting predicate inference for guard functions without the need for explicitly specifying a is clause, it could be interesting to provide a simple helper that allows to get the inferred predicate for any guard function.

Ex.:

function isValid(value: string | number | null | undefined | false) {
  return value != null && value !== false;
} // : value is string | number

type Valid = Predicate<typeof isValid> // string | number

Type source

export type Predicate<T> = T extends (x: any) => x is infer U ? U : never;

Search existing types and issues first

  • I tried my best to look for it

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions