Code Monkey home page Code Monkey logo

Comments (6)

mcriley821 avatar mcriley821 commented on May 5, 2024

I gave this a try with a different syntax for overloads:

declare function bar(
  ...args: [arg0: string, arg1: number, arg2: boolean] | [arg0: number, arg1: boolean, arg2: string] | [arg0: boolean, arg1: string, arg2: number]
): void;

This example is even worse. The editor correctly finds the 3 overloads, but bar(5, and bar(5, ) both fail to resolve to the second overload.

from typescript.

Andarist avatar Andarist commented on May 5, 2024

The difference lies in how callIsIncomplete gets computed in both scenarios:
https://github.dev/microsoft/TypeScript/blob/0b71b81d7d053a3acd379ff49550b3c67f9ce3cd/src/compiler/checker.ts#L33830-L33831
and how, based on that, the version without parenthesis returns true from hasCorrectArity:
https://github.dev/microsoft/TypeScript/blob/0b71b81d7d053a3acd379ff49550b3c67f9ce3cd/src/compiler/checker.ts#L33845-L33849

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on May 5, 2024

I don't think it's really possible to define what the "correct" overload is for an argument set that doesn't match anything. When the parens are still open it makes sense to pretend the future params might be there, but when it's closed, well, who can say what kind of mistake you're making. For example, if there's an overload of one parameter of type "foo" and you wrote func("oo") it seems like we should show you "foo" rather than some other possible overload

from typescript.

mcriley821 avatar mcriley821 commented on May 5, 2024

@RyanCavanaugh not really sure I agree with you. "oo" doesn't match "foo", so why would you show me "foo"?

Overload matching should be best effort matching, no?

In my example, the argument list isn't done, because the comma is indicating I intend to add more arguments.

Not adding another argument is a syntax error. Removing the comma is a compiler error. In either of these choices, it's an invalid call. Why complicate things by not even telling me what the next argument should be?

from typescript.

mcriley821 avatar mcriley821 commented on May 5, 2024

In cases where you have trailing commas in an argument list, you'd have a compiler error if you couldn't find the right overload, so I'd agree here that it wouldn't matter which overload you show.

declare function foo(x: number): void;
declare function foo(x: "foo"): void;

foo(
  "oo",  // error: No overload matches this call.
)

But this is a specifically talking about functions with one parameter.

The main issue I'm trying to outline in my example is that there are multiple parameters, with a correct first argument, yet the overload is not resolved/narrowed.

from typescript.

typescript-bot avatar typescript-bot commented on May 5, 2024

This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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.