stampit-org / stampit

OOP is better with stamps: Composable object factories.
https://stampit.js.org
MIT License
3.02k stars 102 forks source link

[WIP] typescript definitions #347

Closed PopGoesTheWza closed 4 years ago

PopGoesTheWza commented 4 years ago

Issue #245 [Work In Progress]

Types are broken down into two files

For review:

Sample code

import stampit, { compose, init } from 'stampit';

// default instance type (any)

const a = stampit().init(function(options) {
  const a = options.args[0];
  this.getA = () => {
    return a;
  };
});
a(); // Object -- so far so good.
a().getA(); // "a"

// typed instances

  interface CoordsInstance {
    x: number;
    y: number;
    coordsToString: () => string;
  }

  interface ColorInstance {
    color: string;
  }

  const Coords = compose<CoordsInstance>({
    initializers: [
      function({ x, y }) {
        this.x = x;
        this.y = y;
      },
    ],
    methods: {
      coordsToString() {
        return `(${this.x}, ${this.y})`;
      },
    },
  });
  const Point = compose<CoordsInstance & { toString: () => string }>(
    Coords,
    {
      methods: {
        toString() {
          return this.coordsToString();
        },
      },
    },
  );
  const Color = compose<ColorInstance>({
    initializers: [
      function({ color }) {
        this.color = color;
      },
    ],
  });
koresar commented 4 years ago

First of all, this is quite solid ground work! So much new learning for me.

I would leave few comments on the PR, but my TypeScript knowledge is limited (to C#). So, they will likely be irrelevant. :) Sorry!

koresar commented 4 years ago

For streamlining this PR we need to get in sync on stampit's terminology. I'm getting confused with the terminology used in this PR. Let me put it the way I understand:

PopGoesTheWza commented 4 years ago

@koresar All descriptions in specification.d.ts have been reviewed and should now make more sense

PopGoesTheWza commented 4 years ago

This PR is superseeded by #348

PopGoesTheWza commented 4 years ago

Closing this outdated PR