P.select loses type inference when used with multiple patterns in a single .with
Author: salemkodeCreated Aug 23, 2025Updated Sep 15, 2025
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
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(fromP.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 selectedstring.
Versions
- TypeScript version: 5.9.2
Source: gvergnaud/ts-pattern