Lusito / box2d.ts

Full blown Box2D Ecosystem for the web, written in TypeScript
https://lusito.github.io/box2d.ts
60 stars 6 forks source link

Make attributes and methods protected or private again #16

Open Lusito opened 3 years ago

Lusito commented 3 years ago

In flyovers port, almost all members have been made public. Probably due to TypeScript not having a "friend" concept as C++ does.

This should be avoided. Possible alternatives:

class A {
    private x = 0;
}

const a = new A();
a["x"] = 10; // no error

This style should only be used as a last resort internally to imitate the "friend" concept of C++. I usually only use this in tests, where I want to access/modify data, which is otherwise not allowed to be touched.

Update: After trying this a bit, it causes other issues. Like false detection of unused properties (property is only being written, not being read)

Update 2: As said in the comments, I've written idtsc to get this working. The core project has been extended with this technique. The other projects might need adjustments as well.

Projects to adjust:

Lusito commented 3 years ago

Another option would be the internal modifier, which has been a suggestion for quite a while: https://github.com/microsoft/TypeScript/issues/5228

But since it's not implemented yet, an alternative approach would be to add a jsdoc tag, which can then later be used to post-process the generated .d.ts files to turn public into private

Lusito commented 3 years ago

Made a tool for the post-processing: https://github.com/Lusito/idtsc

Now all we need to do is add a jsdoc tag @internal to all internal classes, properties and methods and run this tool after the build step