Skip to content

infer doesn't work correctly with type predicatesΒ #50524

@glevchuk

Description

@glevchuk

Bug Report

πŸ”Ž Search Terms

infer type predicates

πŸ•— Version & Regression Information

  • I've tested this in last 4 versions and i've got same behavior

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface ResponseError {
  type: string;
  data?: Record<string, string>;
}

interface PaymentError extends ResponseError {
  type: "PaymentError";
  data: { reason: string };
}

const isPaymentError = (error: ResponseError): error is PaymentError =>
  error.type === "PaymentError";

const processErrors = <
  TypeGuard extends (error: ResponseError) => boolean,
  ErrorType = TypeGuard extends (error: ResponseError) => error is infer U
  //  =============================================================^
  // A type predicate's type must be assignable to its parameter's type.
  // Type 'U' is not assignable to type 'ResponseError'.(2677)
    ? U
    : unknown
>(
  errors: ResponseError[],
  typeGuard: TypeGuard,
  callback: (error: ErrorType) => void
) => {};

processErrors(
  [],
  isPaymentError,
  (error) => {
  // ^? (parameter) error: PaymentError
    error.data.reason
    //           ^? (property) reason: string
  }
);

πŸ™ Actual behavior

Error on infer declaration, but types inferred correctly

πŸ™‚ Expected behavior

TypeScript doesn't throw an error

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions