question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inference from pattern matching

See original GitHub issue

Would it possible (and would it make sense) to add inference for pattern matching?

For example:

function update(state, action) {
  switch (action.type) {
    case "INCREMENT":
      return state + 1;
    case "DECREMENT":
      return state - 1;
    default:
      return state;
  }
}

let nextState = update(0, { type: "INCREMENT" });

Here, it’s clear from reading the code that action.type has a defined set of expected values.

Hegel does infer from usage that action must have a type property.

But it does not infer that the type is a string - although it’s clear to a person familiar with pattern matching that, not only is a string property expected, but there is also an expected set of constant string types.

I’m sure this is non-trivial, but I’m wondering if it’s even possible and whether it would make sense? 🙂

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
thecotnecommented, Aug 15, 2021

@mindplay-dk that sounds like a good idea

and would imply that this code

function concat(a, b) {
  if (typeof a !== "string") throw new TypeError("a must be a string!");
  if (typeof b !== "string") throw new TypeError("b must be a string!");

  return a + b;
}

will produce this interface

(string, string) => string

instead of this one

(unknown, unknown) => string throws TypeError

which would be very good for inferring types of js code instead of using poorly implemented .d.ts files from DefinitelyTyped

2reactions
mindplay-dkcommented, Aug 9, 2021

It says default: return state - so it should be 'INCREMENT' | 'DECREMENT', but it’s not.

so why do you have default case if only INCREMENT and DECREMENT is allowed?

Because JavaScript? Type-checking is compile-time, so somebody will always be able to pass anything.

Inference from usage seems to be about “what could the developer possibly mean” - and in this particular case, it seems less likely you’d want action.type constrained to a type that the function can actually handle. Otherwise, you’d have declared a (wider) type, right?

that function can handle any object as action even one without type property since there is default: return state

so constraining action.type to something that would make default: return state unreachable branch would not make sense

But there’s no such thing as an exhaustive switch/case in JS - so no matter what I do, handling default or not, you can always pass anything to this function. Even if for default there was a throw statement, it’s still a branch, there’s still a control flow, and therefore a type, in this case just an exception.

Reducer patterns like these are super common in JS now - is there some way we can handle them?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pattern‐matching and inference in story understanding
In SAM, a method of pattern-matching is used, first to decide to which situation a story refers, then to follow the story through...
Read more >
Pattern Matching - The Scala Programming Language
Pattern matching tests whether a given value (or sequence of values) has the shape defined by a pattern, and, if it does, binds...
Read more >
Type inference for unique pattern matching - ACM Digital Library
Regular expression patterns provide a natural, declarative way to express constraints on semistructured data and to extract relevant information from it.
Read more >
Type inference for Haskell, part 14 - Jeremy Mikkola
There's another way to match integers called an “n + k” pattern. This matches any positive integer that is greater than or equal...
Read more >
Type Inference for Regular Expression Pattern Matching
Abstract: An important feature of statically typed XML programming languages is the type inference of variables in regular expression patterns, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found