-
Notifications
You must be signed in to change notification settings - Fork 14
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
TypeScript mixins #1127
Comments
This issue is largely an extension of phetsims/phet-info#143. Given your findings above, I feel like the most prudent thing to do is to convert ParallelDOM to a new-style mixin. This should be simple because it only has a single usage. This will remove 40 compile errors in ratio and proportion |
@jonathanolson also identified some constraints on TypeScript mixins not being able to use private/protected members, but I'm having trouble understanding/replicating that problem. |
Notes from my discussion with @jonathanolson
|
@jonathanolson and I talked briefly about phetsims/scenery@89b51f0 and feel good about the change. |
Constructor arguments work, as long as we parse them out from Mixins/traits with generic type attributes are working nicely. I added a demo of Poolable to Mixable.ts, it is able to have an impressive level of strict typing (createFromPool/etc. get typed arguments and name hints in the IDE). |
Has the pattern recommended in Programming Typescript (p. 101) been evaluated? The book claims that its “easy” to implement mixins/traits in TS, then has an extended example. There’s no mention of problems with EDIT: The book's example looks similar to https://www.typescriptlang.org/docs/handbook/mixins.html. The example in that link does have this comment about private/protected: // Mixins may not declare private/protected properties
// however, you can use ES2020 private fields |
@jonathanolson can you please review the following 3rd party libraries and evaluate whether they would be a good match for our project? https://github.com/tannerntannern/ts-mixer |
@jonathanolson and I discussed this today and feel like steps like these would be good:
The main problems with the approach in wilder at the moment are:
We could always start with the wilder pattern then switch to another pattern if we find a better one. We can't apply it in common code until common code is using TypeScript. Until then, we have the
|
I tested changing Imageable like so: type Constructor = new ( ...args: any[] ) => {};
/**
* @param {constructor} type
*/
const Imageable = <Base extends Constructor>( type: Base ) => {
return class extends type { Then I was able to create an Image, and access elements from Node, Image and Imageable, and Unassigning until we have TypeScript in common code. |
Can the same pattern that was used for |
It seems appropriate to use ts-ignore until we enable TypeScript in common code. Self-unassigning for now. |
Hello mixin issue! We have not updated this in quite some time, but lots of progress has been made. @zepumph took the lead on phetsims/scenery#1340, and @jonathanolson has converted Imageable, Paintable, and Poolable to typescript in the last quarter. We have established a pattern of how we want Typescript mixins to look (when possible) in our project, and documented it in https://github.com/phetsims/phet-info/blob/master/doc/phet-software-design-patterns.md#mixin-and-trait. Tagging @jessegreenberg and @jonathanolson in case they have anything else to provide here. I'm going to close, but feel free to reopen if there are open questions or concerns that have not been addressed. |
Related to #1049, I set up a model of mixins following the new approach we have been using, which matches the recommended style in TypeScript https://www.typescriptlang.org/docs/handbook/mixins.html
Here's a patch that demonstrates this pattern using Node, PhetioObject and a ParallelDOM mixin, and a Node subtype of Panel:
The usage site looks like this:
and has correct type checking and navigation in the IDE and correct type checking from
tsc
. So this seems like a good pattern we can continue using.The mixin declaration looks like this:
If writing these files in *.js, they are inferred as
any
and while it passes the type checking, it also doesn't help. So we would need to use *.ts for those, or investigate JSDoc to get it to not infer asany
if that is possible.The text was updated successfully, but these errors were encountered: