1

For example, in:

const getLoc = new Promise(navigator.geolocation.getCurrentPosition);

Typescript seems to infer the type Promise<Position> correctly without error, but the browser gives:

Unhandled Rejection (TypeError): 'getCurrentPosition' called on an object that does not implement interface Geolocation.

if we eta expand to

const getLoc = new Promise((r,e) => navigator.geolocation.getCurrentPosition(r,e));

Then it runs without issue, but this seems unnecessarily verbose, and screws with type inference (gives Promise<unknown> instead without manual annotation). So I want to understand what's actually going on.

I suspect it has something to do with the binding of this but I have no idea what to search for to figure it out.

I think the same thing is going on for this question, but there's no explanation to the solution.

Dai
  • 41
  • 6
  • 2
    A promise constructor invokes the passed function as `f();`, so the `navigator.geolocation` context is lost. – zerkms Mar 12 '20 at 21:07
  • 2
    The `getCurrentPosition` method appears to contain within its implementation an expectation for the target of the function. In order to meet this expectation, the function must be called such that its target is `navigator.geolocation`. You could use `bind`: `const getLoc = navigator.geolocation.getCurrentPosition.bind(navigator.geolocation)`. There is a proposal that would improve this syntax: `const getLoc = ::(navigator.geolocation).getCurrentPosition` (https://github.com/tc39/proposal-bind-operator) – Ben Aston Mar 12 '20 at 21:51

0 Answers0