Skip to content

P.select loses type inference when used with multiple patterns in a single .withΒ #331

@salemkode

Description

@salemkode

Describe the bug

When using P.select(P.string) with multiple patterns in the same .with clause, the handler parameter is not inferred as string.

Instead, it is inferred as the union of the full object shapes, rather than the selected value.


TypeScript playground with a minimal reproduction case

Playground Link

import { match, P } from "ts-pattern";

const obj = {} as unknown;

const message = match(obj)
  .with(
    { errors: P.select(P.string) },
    { description: P.select(P.string) },
    (message) => {
      console.log("message", message);
      // ❌ Expected: message is string
      // ❌ Actual: message is inferred as object type
      return `Please enter ${message.replace("length", "characters")}`;
    }
  )
  .otherwise(() => "");

Expected behavior

  • The handler parameter should be inferred as string (from P.select(P.string)), even when multiple patterns are used.

Actual behavior

  • The handler parameter is inferred as the union of the object shapes ({ errors: string } | { description: string }), not the selected string.

Versions

  • TypeScript version: 5.9.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions