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
Describe the bug
When using
P.select(P.string)with multiple patterns in the same.withclause, the handler parameter is not inferred asstring.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
Expected behavior
string(fromP.select(P.string)), even when multiple patterns are used.Actual behavior
{ errors: string } | { description: string }), not the selectedstring.Versions