phetsims / natural-selection

"Natural Selection" is an educational simulation in HTML5, by PhET Interactive Simulations
GNU General Public License v3.0
3 stars 7 forks source link

Problems with BunnyIO #327

Closed pixelzoom closed 1 year ago

pixelzoom commented 1 year ago

Discovered during https://github.com/phetsims/natural-selection/issues/326 TypeScript conversion...

There are mutliple PhET-iO problems with Bunny.ts. I'll need assistance from @samreid or @zepumph for this issue.




      // genotype and phenotype are stateful and will be serialized automatically.

  private static getStateSchema( BunnyIO: IOType ): CompositeSchema { ... }

  public static readonly BunnyIO = new IOType( 'BunnyIO', {
    valueType: Bunny,
    stateSchema: Bunny.getStateSchema, // TS2322 error here

... and that results in this TS error for the last line:

TS2322: Type '(BunnyIO: IOType<any, any>) => CompositeSchema' is not assignable to type 'StateSchemaOption<any, any> | undefined'.   Type '(BunnyIO: IOType<any, any>) => CompositeSchema' is not assignable to type '(ioType: IOType<any, any>) => StateSchema<any, any>'.     Type 'CompositeSchema' is missing the following properties from type 'StateSchema<any, any>': displayString, validator, compositeSchema, validateStateSchema, and 6 more.

I've also tried this:

  private static getStateSchema( BunnyIO: IOType ): StateSchema<Bunny, BunnyStateObject> {
    return {

      // Even though father and mother are stateful, we need a reference to them.
      father: NullableIO( ReferenceIO( BunnyIO ) ), // TS2322 error here

... but that results in this TS error for the last line:

TS2322: Type '{ father: IOType<any, any>; mother: IOType<any, any>; generation: IOType<number, number>; isAlive: IOType<boolean, boolean>; age: IOType<number, number>; _private: { ...; }; }' is not assignable to type 'StateSchema<Bunny, BunnyStateObject>'.   Object literal may only specify known properties, and 'father' does not exist in type 'StateSchema<Bunny, BunnyStateObject>'.


pixelzoom commented 1 year ago

Natural Selection is not currently a publication priority, so unassigning.

pixelzoom commented 1 year ago
-  public static readonly BunnyIO = new IOType( 'BunnyIO', {
+  public static readonly BunnyIO = new IOType<Bunny, BunnyStateObject>( 'BunnyIO', {
samreid commented 1 year ago

As part of https://github.com/phetsims/tandem/issues/297, @zepumph and I made it possible for composite state schemas to use underscore keys (as well as continuing to support non-underscored keys), and to access underscored or non-underscored class attributes. There is a TODO that relates to this:

export default class Bunny extends Organism {

  //TODO https://github.com/phetsims/natural-selection/issues/327 father, mother, isAlive, age should be readonly for clients
  public father: Bunny | null;
  public mother: Bunny | null;
  public readonly generation: number;

In this case, if desired, you could rename those like: _father etc and have a public getter (and no public setter). You will likely still need to leave it non-readonly if you want to null it out in dispose. But _father it could be private.

pixelzoom commented 1 year ago

Changes completed in the above commits. Tested in State wrapper. Migraton wrapper indicates that no additional migration rules are needed.

Closing.