Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export typings correctly in package.json #2438

Closed
chucksellick opened this issue May 19, 2023 · 9 comments
Closed

Export typings correctly in package.json #2438

chucksellick opened this issue May 19, 2023 · 9 comments

Comments

@chucksellick
Copy link

See this ticket in TypeScript for discussion:

microsoft/TypeScript#52363

I am having this issue using Turfjs in a project with latest Vite/Typescript/React. It refuses to build because the typings need exporting correctly in package.json when I am building in Vite's "bundler" mode.

@MorpheusXAUT
Copy link

Looks like this was already noticed a few months ago in #2414.
A fix was seemingly already provided in PR #2400, however no version containing this fix has been released yet, I assume since preparations for v7.0.0 are still ongoing...

Would it be possible to get a 6.X.Y release containing this fix so we can continue to consume the package while work on v7.0.0 is still ongoing? 😃

@vpctorr
Copy link

vpctorr commented Jul 7, 2023

In the meantime, here's a quick patch if anyone needs it
https://gist.github.com/vpctorr/5217a668544e88790e545cbe6914d34e

@twelch
Copy link
Collaborator

twelch commented Sep 20, 2023

This fix should be available in 7.0.0-alpha.1, feedback welcome confirming it is resolved.

@abettermap
Copy link

feedback welcome confirming it is resolved

Confirmed using Vite 4.3.9! I was able to drop a bunch of ts-ignore's from my imports, and re-strengthen a bunch of Turf types.

I upgraded from v6, however, and ran into some other snags. I could not find a migration guide, so if it helps others here were the snags:

  • turf.intersect now takes a FeatureCollection rather than a pair of Poly/Multipoly geoms in the old version.
  • Typings like turf.Geometry are no longer a thing. This was fine as I simply switched to GeoJSON.Geometry, but I believe my original reason for using the Turf-specific type was some kind of fight against GeoJSON.GeometryCollection. Seems to work without a hitch now, though.
  • Had to do a few coercions, e.g. turf.feature(boundary as GeoJSON.Geometry), but that could also be related to recent changes to my project's backend API/OpenAPI so take this one with a grain of salt.
  • pointOnSurface is no longer a thing. I simply refactored to use pointOnFeature instead.

I am not using Turf very heavily, like maybe 20ish modules, but just wanted to point out those little snags in case it helps anyone (even if it's not related to the Vite fix, which is working great 🙌).

@abettermap
Copy link

Spoke too soon

...but it might just be something specific to my setup. Vite and my application work just fine during development, but my tests will not run properly for any test that imports a file that uses turf. Example:

FAIL  src/components/reusable/maps/map-ctrls/measure/utils.test.ts [ src/components/reusable/maps/map-ctrls/measure/utils.test.ts ]
SyntaxError: Named export 'BufferOp' not found. The requested module '@turf/jsts' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@turf/jsts';
const { GeoJSONReader, BufferOp, GeoJSONWriter } = pkg;

 ❯ src/components/reusable/maps/map-ctrls/measure/utils.ts:1:31
      1| import {
      2|   area as turfArea,
      3|   center as turfCenter,
       |  ^
      4|   feature as turfFeature,
      5|   length as turfLength,

What I've tried

Very possible I am doing something dumb on my end, but here are my unsuccessful troubleshooting attempts so far:

  • change all import * as turf from "@turf/turf" instances to import turf from "@turf/turf"
  • change a few instances of that to things like this:
import { area, center, feature, length, } from '@turf/turf'

Environment

Not sure if relevant, but:

"jest": "~28.1.3"
"vite": "^4.3.9"
"vitest": "^0.31.1"
╰─ yarn -v && node -v && npm -v
1.22.19
v18.7.0
8.15.0
// Package.json has this as its type:
"type": "module"

I realize this issue is not Vite-specific, I'm just adding everything for reference. Please let me know what else I can test/change, happy to do so.

@MorpheusXAUT
Copy link

@abettermap I don't have a solution, but just wanted to chime in to confirm I've run into the exact same points you have (e.g. the GeoJSON types), including the BufferOp issue.

Running on Node v18.16.0, Typescript ^5.2.2 with "type": "module" and a TS target of ESNext and module/moduleResolution of NodeNext. No vite or similar involved.

I've tried similar debugging steps as you have - unsuccessful as well unfortunately - and ended up reverting to a patched fork of v6.5.0 because of that.

@twelch
Copy link
Collaborator

twelch commented Sep 25, 2023

@abettermap - thanks so much for this feedback! Turf v7 builds now on top of @types/geojson, as you noticed, you no longer import base types from Turf, rather you import them from @types/geojson and Turf types should be compatible as you've found.

From what I can tell the original issue in this ticket has been resolved is that correct? I think the BufferOp issue should be opened as a new issue. Would you be willing to open it? The simplest reproducible example would be helpful.

@abettermap
Copy link

I think the BufferOp issue should be opened as a new issue. Would you be willing to open it? The simplest reproducible example would be helpful.

Done, but the only way I know to reproduce it is Vite-based, which probably doesn't help you isolate the issue, but maybe MorpheusXAUT's "no vite or similar" comment will shed some light.

@abettermap
Copy link

Sorry Tim, I forgot to answer your question. Yes, I believe the issue in the ticket has been resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants