alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
546 stars 56 forks source link

Typescript destructure import support #98

Open justin-hackin opened 2 years ago

justin-hackin commented 2 years ago

I'm not sure why the type definitions aren't working when destructuring the default import but I always get TS2614 error when importing

Module '"../node_modules/@flatten-js/core"' has no exported member 'Line'. Did you mean to use 'import Line from "../node_modules/@flatten-js/core"' instead?ts(2614)

Here's a reproduction: https://codesandbox.io/s/dark-sound-r7148?file=/src/index.ts

It's been like this since I started using the package several years ago.

justin-hackin commented 2 years ago

I noticed that you can work around this by const-destructuring the imports like this: https://codesandbox.io/s/infallible-solomon-tyj8p?file=/src/index.ts . Perhaps this warrants a notice in the readme

verekia commented 1 year ago

This is how I fix it with re-exports:

flatten.ts

// This file exists to fix destructured imports in Flatten
// https://github.com/alexbol99/flatten-js/issues/98

import Flatten from '@flatten-js/core'

const {
  Box,
  Point,
  Ray,
  Vector,
  Segment,
  point,
  circle,
  // @ts-ignore
  box,
  segment,
} = Flatten
export type FlattenBox = Flatten.Box
export type FlattenPoint = Flatten.Point
export type FlattenRay = Flatten.Ray
export type FlattenVector = Flatten.Vector
export type FlattenSegment = Flatten.Segment
export type FlattenCircle = Flatten.Circle

// Missing in their typedefs
type box = (xmin?: number, ymin?: number, xmax?: number, ymax?: number) => FlattenBox

export { Box, Point, Ray, Vector, Segment, point, circle, box, segment }

(add all the shapes you need)

Then I can do :

import { box, FlattenBox } from './flatten'