gvergnaud / ts-pattern

🎨 The exhaustive Pattern Matching library for TypeScript, with smart type inference.
MIT License
12.5k stars 135 forks source link

Matching on union property doesn't narrow the property #265

Closed LBBO closed 5 months ago

LBBO commented 5 months ago

Describe the bug When I try matching over a type that has a union property, I get a different result than when I match over a union type with the same property:

// Doesn't work
type Obj = {
    type: "foo" | "bar"
}

// Works
// type Obj =
//     | {
//           type: "foo";
//       }
//     | {
//           type: "bar";
//       };

declare const obj: Obj;

match(obj)
    .with({ type: "foo" }, () => "adsf")
    .otherwise(({ type }) => type satisfies 'bar'); // type is not narrowed correctly with the first Obj type

I understand that this may be due to some limitation, but if that's the case, perhaps the docs could be adjusted to explain this limitation explicitly.

TypeScript playground with a minimal reproduction case

Example: Playground

Versions

gvergnaud commented 5 months ago

That's expected. Duplicate of #145, #182 details here

LBBO commented 5 months ago

Sorry for the duplicate and thanks for the links!