GithubHelp home page GithubHelp logo

Comments (4)

JUSTIVE avatar JUSTIVE commented on June 8, 2024 1

you should use narrowed instances to benefit from pattern matching.

match(object)
    .with({type:"variant1"}, (object) => {
      console.log(object.data.someField);
      console.log(object.flavor);
    })
    .with({type:"variant2"}, (object) => {
      console.log(object.data.length);
      console.log(object.kind);
    })
    .exhaustive();
};

from ts-pattern.

ian-schu avatar ian-schu commented on June 8, 2024 1

Thanks guys! I had no idea I was supposed to be matching on the whole object. It's interesting that the native TS switch will do this exactly as I hoped 🤔

Anyway, I am unblocked on this 👍🏻

Really interested in the TS 5.5 predicate inference. Seems like that would allow match() to behave a lot more like the native switch that I gave in my sandbox.

from ts-pattern.

gvergnaud avatar gvergnaud commented on June 8, 2024

Yes, as @JUSTIVE showed, you need to match on the whole object and use the narrowed reference that's provided as a parameter to handler functions.

I wish there was a way to narrow an object that's in scope based on the pattern given to .with, but it isn't possible today.

from ts-pattern.

JUSTIVE avatar JUSTIVE commented on June 8, 2024

maybe there's some point where we could benefit from typescript 5.5's type predicate inference. not sure how we'll benefit from it, but since it automatically narrows type, I'm pretty sure there's some way.

from ts-pattern.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.