polybooljs icon indicating copy to clipboard operation
polybooljs copied to clipboard

Add types declaration file

Open geopic opened this issue 4 years ago • 13 comments

Resolves #20. Let me know if there are any issues.

geopic avatar Mar 07 '20 09:03 geopic

Yes please! I need this for it to work in Typescript. Are there any holdups to prevent this pull request?

dirkesquire avatar Jun 02 '20 20:06 dirkesquire

Yes, sorry for the delay -- the types are probably good enough for the public API, but the details are wrong.

For more accurate types, I would like segments and combined be separate opaque types.

velipso avatar Jun 02 '20 21:06 velipso

The Flavor strategy in this blog post might be a good way to do opaque types in TS:

https://spin.atomicobject.com/2018/01/15/typescript-flexible-nominal-typing/

Something like:

type Segments = { _type?: 'PolyboolJsSegments' };
type CombinedSegments = { _type?: 'PolyboolJsCombinedSegments' };

velipso avatar Jun 02 '20 21:06 velipso

I'm honestly struggling to get this project working with Typescript at all. Are there any other options I can use in the meantime?

dirkesquire avatar Jun 06 '20 16:06 dirkesquire

Unfortunately what @voidqk has suggested is outside of my knowledge at the moment. What I've submitted works according to my own tests but may not work (or only work partially) elsewhere. So I can't be of much more help than this. :man_shrugging:

geopic avatar Jun 06 '20 18:06 geopic

Thanks geopic. So it does work for you? I'm just not smart enough to figure out why this is not working. What I've done is add polybooljs to my project and then to manually create a file polybooljs/index.d.ts and placed the contents of your file from the pull request in there. But I get these errors:

ERROR in [at-loader] ./src/start.ts:125:1 
    TS2693: 'PolyBool' only refers to a type, but is being used as a value here.

ERROR in [at-loader] ./src/start.ts:126:7 
    TS2339: Property 'union' does not exist on type 'typeof import("polybooljs")'.

This is my source code. I just can't seem to call the union function no matter what I try:

import PolyBool from 'polybooljs'
import * as PolyBool2 from 'polybooljs'
import { Polygon, Segment, GeoJSON } from 'polybooljs'

let poly1: Polygon = {
    regions:  [[ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ] ]],
    inverted: false
}

let poly2: Polygon = {
    regions: [[ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ] ]],
    inverted: false
}

PolyBool.union(poly1, poly2)         // Gives first error
PolyBool2.union(poly1, poly2)      // Gives second error

dirkesquire avatar Jun 06 '20 18:06 dirkesquire

Ok, on second thought, I'll look at it again. I think I know what's up. My IDE sometimes complains about these things then sometimes doesn't (when it should). I'll give it another look and will submit a squashed commit on Monday unless I cannot make it work, in which case I'll let you guys know. Thanks for the heads up @dirkesquire.

geopic avatar Jun 06 '20 20:06 geopic

Ok, I've rebased onto the previous commit so it should still be one commit being added. There were some issues with the file I wrote back in March which I've fixed and now it works 100% with the TypeScript compiler. The types themselves are my best guess but at least the types file is structured properly now. I believe you shouldn't have any more issues @dirkesquire but let me know if you do. :+1:

geopic avatar Jun 08 '20 04:06 geopic

Thanks, this works for me!

dirkesquire avatar Jun 09 '20 10:06 dirkesquire

Guys, let's merge this PR. I need it in production 🙂.

pahuta avatar Dec 07 '20 07:12 pahuta

Merge it y'all!

alburdette619 avatar Feb 22 '21 18:02 alburdette619

Paging @velipso

geopic avatar Feb 22 '21 19:02 geopic

The types aren't correct... I haven't looked at this code in a while, but Segments is wrong, and combine is currently returning combined: object, which is also wrong...

I realize that you don't need these types to be accurate for regular usage, but I'm not going to merge it if the types are wrong. I also don't understand the urgency of merging the types... you can drop your own types into TypeScript using .d.ts files. TypeScript is specifically designed to allow third-party types. If this file is good enough for your usage, grab it, and drop it into your build.

Also, feel free to fork and land the types if you really want to. I don't look at this repo often.

velipso avatar Feb 22 '21 19:02 velipso