Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exhaustiveness check fails for object patterns with typos #44

Closed
bdrobinson opened this issue Aug 20, 2021 · 2 comments
Closed

Exhaustiveness check fails for object patterns with typos #44

bdrobinson opened this issue Aug 20, 2021 · 2 comments

Comments

@bdrobinson
Copy link

Describe the bug
When pattern matching against multiple attributes in an object, ts-pattern will throw a runtime error that the exhaustive pattern matching was not able to spot at build-time if one of the attributes is misspelled.

Code Sandbox with a minimal reproduction case
https://codesandbox.io/s/elated-wilbur-h0mz2?file=/src/index.ts
(copy+pasted too):

import { match } from "ts-pattern";

type Person = {
  sex: "Male" | "Female";
  age: "Adult" | "Child";
};

function summary(person: Person): string {
  return (
    match(person)
      // Typo – "agf" should be "age"
      .with({ sex: "Female", agf: "Adult" }, () => "Woman")
      .with({ sex: "Female", age: "Child" }, () => "Girl")
      .with({ sex: "Male", age: "Adult" }, () => "Man")
      .with({ sex: "Male", age: "Child" }, () => "Boy")
      // there is no TS error
      .exhaustive()
  );
}

// yet this throws an error at runtime :(
console.log(summary({ sex: "Female", age: "Adult" }));

Versions

  • TypeScript version: 4.3.5
  • ts-pattern version: 3.2.4
  • environment: browser + version / node version / deno version

Thanks – this library is great!

@gvergnaud
Copy link
Owner

Thanks, I'll take a look

@gvergnaud
Copy link
Owner

This has been fixed in v3.2.5!

Code sandbox: https://codesandbox.io/s/nifty-moore-pwxx7?file=/src/index.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants