onekiloparsec / aa-js

The most comprehensive collection of accurate astronomical algorithms in JavaScript (TypeScript).
https://onekiloparsec.dev/aa-js/
MIT License
39 stars 5 forks source link

ts7016 error - cannot resolve types between index.d.ts and package.json #94

Open sxflynn opened 2 months ago

sxflynn commented 2 months ago

I performed an npm install aa-js into my Typescript project and VS Code is giving this linting error on the import statement.

I followed the example in the readme and started by importing julian days.

import { juliandays, Earth } from 'aa-js'

const jd = juliandays.getJulianDay(new Date())

Under the 'aa-js' it provides an error and this is the message:

Could not find a declaration file for module 'aa-js'. '/[appname]/node_modules/aa-js/dist/aa-js.js' implicitly has an 'any' type. There are types at '[appname]/node_modules/aa-js/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'aa-js' library may need to update its package.json or typings. ts(7016)

For example, this code does work:

import {
  Sun,
  coordinates,
  type JulianDay,
  GeographicCoordinates,
  HorizontalCoordinates,
  EquatorialCoordinates,
} from "aa-js";

export const calcSunAltitude = (
  jd: JulianDay,
  coord: GeographicCoordinates
): HorizontalCoordinates => {
  const sunGeoEquCoord: EquatorialCoordinates =
    Sun.getGeocentricEquatorialCoordinates(jd);
  return coordinates.transformEquatorialToHorizontal(jd, sunGeoEquCoord, coord)
    .altitude;
};

Although I am able to compile and run the web application with this error, I am not provided with any Typescript intellisense or typechecking.

sxflynn commented 2 months ago

Update:

In my tsconfig.json file I manually pointed to the aa-js directory in node_modules and this resolved the error.

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "aa-js": ["node_modules/aa-js/dist/index.d.ts"]
    },
// rest of options
   }
}

That being said, this should not be necessary. The package.json in the aa-js package needs to be modified so that it can point to types without the consumer modifying their local configuration.

onekiloparsec commented 2 months ago

Thank you very much for your bug report. I must admit I always had a very hard time understanding how to configure TS! Seriously, I read many articles, and the official documentation, but I always got lost.

I totally agree with your conclusion: this should not be necessary. However, I have no idea how to solve it.