Skip to content

CallableFunction inference does not match runtime behavior #30294

Description

TypeScript Version: 3.4.0-dev.20190309

Search Terms: CallableFunction, call, bind, inference

Code

Promise.resolve.call(1)           // Compiles without error, throws in runtime
Promise.resolve.call(Promise, 1); // Compile-time error, works fine in runtime

Expected behavior:

  • Promise.resolve.call(1) should trigger a compile-time error
  • Promise.resolve.call(Promise, 1) should compile

Actual behavior:

The situation is reversed. The one that works in runtime does not compile. The one that does compile throws a runtime error.

  • Promise.resolve.call(1) compiles without error, throws Uncaught TypeError: PromiseResolve called on non-object in runtime
  • Promise.resolve.call(Promise, 1) gives a compile-time error (Expected 1 arguments, but got 2.), works fine in runtime (returns a Promise<number>)

I'm using "lib": ["dom", "esnext"], "target: "es2015". Tested on Chrome 72.0.3626.121, V8 7.2.502.28.

Playground Link: The compile-time error is not there, but the runtime exception can be observed

Activity

  1. RyanCavanaugh commented on Mar 19, 2019

    @RyanCavanaugh
    Member
  2. rbuckton commented on Mar 19, 2019

    @rbuckton
    Contributor

    Ryan Cavanaugh (@RyanCavanaugh): This is likely due to overload resolution related to --strictBindCallApply, as we see the same error in this case:

    declare class C {
      static x(y: number): number;
      static x(): number;
    }
    C.x.call(C, 1); // error, but should be ok
    C.x.call(C); // ok
    C.x.call(1); // ok (not an error because the `this` type of `x` is not constrained)
  3. rbuckton commented on Mar 19, 2019

    @rbuckton
    Contributor

    Note that if you switch the order of the overloads, you get the exact opposite results (C.x.call(C, 1) works but C.x.call(C) and C.x.call(1) fail).

  4. rbuckton commented on Mar 19, 2019

    @rbuckton
    Contributor

    Ryan Cavanaugh (@RyanCavanaugh): Not a bug per se, as we have documented this is the expected behavior, however it does result in a poor user experience.

  5. added
    Design LimitationConstraints of the existing architecture prevent this from being fixed
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions