-
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
Notes on bringing TS code up to the current standards #1281
Comments
I'm not going to create a new doc, because this list is specific to this sprint (July 14-20, 2022), and it mostly references other existing docs. For this TS sprint, here's what I feel are the priorities for bringing your code up to current standards: (1) Clean up (2) Document options properties (fields) where they appear in the (3) Document class properties (fields) where they are declared, not where they are instantiated. See https://github.com/phetsims/phet-info/blob/master/doc/typescript-conventions.md#documentation. (It's OK, and encouraged, to document the significance of a specific initial value where it's instantiated.) (4) Prefer more-general types for APIs (fields, method parameters, return values) to hide implementation details and make the API more general - especially in common code! A couple of very general examples are shown below, and keep in mind that API design is all about context. NOTE: Most of the confusion that I see in APIs tends to be related to the "Property" hierachy, so ask questions in Slack#typescript. /**
* In this example, our API needs to expose the number of particles that are in our particle system,
* so that the value can be displayed in the UI.
*/
class ParticleSystem {
// Too specific because callers do not need the additional functionality of NumberProperty.
// For example, a caller might rely on numberOfParticlesProperty.rangeProperty, which is not
// something that we intended to expose.
public readonly numberOfParticlesProperty: NumberProperty;
// Preferred because it meets the requirements, does not expose unnecessary features of
// how the Property is implemented, and makes it easy for us to change the implementation
// in the future.
public readonly numberOfParticlesProperty: IProperty<number>;
constructor( ... ) {
...
this.numberOfParticlesProperty = new NumberProperty( 0, {
numberType: 'Integer',
range: new Range( 0, 100000 ),
tandem: options.tandem.createTandem( 'numberOfParticlesProperty' )
} );
}
}
/**
* In this example, MeterNode has an associated icon. The appearance of the icon is intended
* to be immutable (cannot be changed) and used to label checkboxes, buttons, etc. The icon
* can be transformed (translated, scaled, etc.)
*/
class MeterNode {
// Too specific because it exposes features of Path that are unnecessary for the requirements,
// could be used/abused by callers, and limit our ability to change the implementation in the future.
// For example, a caller could totally change the icon's appearance by setting icon.shape.
public readonly icon: Path;
// Preferred because it meets the requirements, does not expose unnecessary features of
// how the icon is implemented, and makes it easy for us to change the implementation in the future.
public readonly icon: Node;
constructor( ... ) {
this.icon = new Path( ... );
}
} (5) Convert deprecated enumerations to |
From today's meeting, we want to schedule a discussion around that. Perhaps related to articles like UPDATE: I also saw this article: https://stackoverflow.com/questions/16934214/accept-the-weakest-return-the-strongest-but-why which says:
|
7/19/2022 TS meeting: We had a discussion about (4) in #1281 (comment). Summary:
|
In Slack, @AgustinVallejo asked me about parameters.
Parameters should be typed based on the needs of the implementation, not based on the type of the arguments that happened to be used when the method was implemented. Here's an example for
Questions? |
At 7/21/2022 dev meeting, @kathy-phet said she'd like to keep this issue open, so that information herein can be rolled into other documentation as appropriate (conventions, onboarding, etc.). Assigned to @AgustinVallejo to lead that effort. |
I've assigned myself to this, and will work with @AgustinVallejo to make sure that we have captured the information above in our TypeScript standards doc(s). |
@jbphet and @AgustinVallejo, looks like we added this to the onboarding documentation. Can this be wrapped up and closed now? |
As part of the TS sprint...
@kathy-phet would like me to take a making notes (a prioritized list of things to consider?) when bringing TS code up to the current standards.
The text was updated successfully, but these errors were encountered: