Coder Social home page Coder Social logo

Comments (2)

Andarist avatar Andarist commented on June 2, 2024 4

I love fun issues. Expect me digging into this one later 😉

You have my attention

from typescript.

Andarist avatar Andarist commented on June 2, 2024 3

So in this case we end up with multiple inference candidates. We have:

  • covariant: [any]
  • contravariant: [] and [a: number]

When multiple inference candidates are present, TS tries to select the best match based on different criteria. Since contravariant candidates don't overlap sufficiently enough the first one gets picked as overall inferredContravariantType. Not: This is order-dependent so when you switch the order of ur union members it magically works because the other one gets picked there.

Since we have both inferredCovariantType and inferredContravariantType we also need to pick between those 2. It happens so that preferCovariantType gets computed as false in your case. It's rejected based on this check:

some(inference.contraCandidates, t => isTypeSubtypeOf(inferredCovariantType, t))

[any] is not a subtype of either of the contravariant candidates, it's not a subtype of [] and it's not a subtype of [a: number]. So it ends up picking the covariant candidate and it fails to typecheck ur arguments against inferred signature further down the line.

So it turns out one of my PR fixes this case alrady: #57909

But what happens in the implementation here got me thinking... why do we even have multiple contravariant candidates here? we do have that because the inference infer from each of your signatures separately here. This is kinda a single call though. TS is able to combine union (or intersection) signatures into one. Shouldn't we then infer from that combined signature here?

So that led me to creating this experiment that also addresses this issue: #58482

from typescript.

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.