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

Properly encapsulate ES6 type defs. in module #346

Merged

Conversation

rafern
Copy link
Contributor

@rafern rafern commented Feb 28, 2023

This should fix the weird discrepancy between the type definitions and the actual runtime; doing new Manifold instead of new module.Manifold will now actually show an IDE error.

I'm pretty sure this is correct now, but I'll keep this as a draft so I can take a better look into this tomorrow.

@rafern rafern marked this pull request as ready for review March 1, 2023 11:41
@rafern
Copy link
Contributor Author

rafern commented Mar 1, 2023

@elalish This should be ready now. I tried to make it work with Monaco, but I can't figure out how; the type definitions are split into multiple files, because we need a public and private version of the Mesh and Manifold classes, but for some reason Monaco doesn't like to cross-reference type definitions. I also can't make it more usable for Monaco by merging the type definitions into a single file, because there is no way to do private type definitions in a .d.ts without multiple files; if I were to use a namespace instead of a separate file, the namespace is auto-exported, meaning you could do, for example, new ManifoldStatic.Manifold, which will result in a runtime error.

Seems like there's 2 choices at the moment:

  • Keep using declare as before, for every function and class inside ManifoldStatic. This should make the type definition usable by Monaco, but also allow invalid usage in Typescript (doing new Manifold instead of new module.Manifold, calling cube() instead of module.cube(), etc...). We'd also have to make it work as a module somehow (maybe re-export?)
  • (the workaround you added yesterday) Keep this version of the type definitions, but have a separate type definition file for the editor. This is more work, but should give correct type definitions everywhere

I also updated the cmake file to copy the new type definition files to the editor's folder. These aren't being used right now, but it should still be useful if a solution is found in the future.

@codecov
Copy link

codecov bot commented Mar 1, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (95daa54) 91.52% compared to head (651729e) 91.52%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #346   +/-   ##
=======================================
  Coverage   91.52%   91.52%           
=======================================
  Files          32       32           
  Lines        3730     3730           
=======================================
  Hits         3414     3414           
  Misses        316      316           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

*/
function reserveIDs(count: number): number;
export type Manifold = T.Manifold;
export type Mesh = T.Mesh;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, do we actually want these? I thought this was what you removed so that new Manifold() would correctly error?

Copy link
Contributor Author

@rafern rafern Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the way it was done before, both the type and the value were exported. In the new way, Manifold and Mesh are re-exported only as a type (which is why it has export type Manifold instead of export class Manifold), so if you try to do new Manifold you will now get an error. It's only a type AND value when used from a ManifoldStatic instance (which is why it's using Manifold: typeof T.Manifold in ManifoldStatic)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thank you for the explanation!

function getCircularSegments(radius: number): number;
///@}
import * as T from './manifold-encapsulated-types';
export * from './manifold-global-types';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea on separating these. I definitely don't want to go back to the declare version. The first priority is getting the types right for npm users - the editor is our own problem. Maybe we could hack it a little though - I'm considering some string concatenation and replacing export with declare during import to the editor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if that would work, because Typescript really hates bundling type definitions, but it's worth a try. Maybe you could use something like dts-bundle?

Copy link
Owner

@elalish elalish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help!

@elalish elalish merged commit 893a4f8 into elalish:master Mar 1, 2023
cartesian-theatrics pushed a commit to SovereignShop/manifold that referenced this pull request Mar 11, 2024
* Properly encapsulate ES6 type defs. in module

* Fixed import file name typo

* Copy type definitions to editor folder (not used for now)

* Dummy commit (trigger CI)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants