chrisveness / geodesy

Libraries of geodesy functions implemented in JavaScript
http://www.movable-type.co.uk/scripts/geodesy-library.html
MIT License
1.18k stars 204 forks source link

Error: An accessor cannot be declared in an ambient context #85

Open minicatsCB opened 4 years ago

minicatsCB commented 4 years ago

Hi, I just started using this library with Vue and TypeScript.

Versions: Node: v12.7.0 Vue: 2.6.11 TypeScript: 3.5.3

Following the instructions in the README.md, I installed (from package.json): "geodesy": "^2.2.1" "@types/geodesy": "^2.2.2" "esm": "^3.2.25"

In one of my Vue components I did:

import LatLon from 'geodesy/latlon-spherical.js';

...

const p1 = new LatLon(50.06632, -5.71475);
const p2 = new LatLon(58.64402, -3.07009);

const d = p1.distanceTo(p2);
console.assert(d.toFixed(3) == '968874.704');

Which works OK. But when I took a look at the console there were many errors like this:

ERROR in /path//node_modules/@types/geodesy/latlon-spherical.d.ts(18,9):
18:9 An accessor cannot be declared in an ambient context.
    16 |     get lng(): number;
    17 |     set lng(lon: number);
  > 18 |     get longitude(): number;
       |         ^
    19 |     set longitude(lon: number);
    20 |     static get metresToKm(): number;
    21 |     static get metresToMiles(): number;

What can be happening? Could I be missing any step?

chrisveness commented 4 years ago

I'm not a TypeScript user, but this appears to be a widespread issue after TypeScript introduced a breaking change in a semver-minor release(!).

From what I can see, it may be resolved by changing the declaration for getters/setters in latlon-spherical.d.ts; e.g. (perhaps)

readonly longitude: number;

in place of

get longitude(): number;

– but as I'm not a TypeScript user, I can't be sure!

I suggest you approach the maintainers of the DefinitelyTyped geodesy types.