Code QuizAdvanced

ThisType for contextual this

Fixing a ThisType marker so methods can see each other and the data.

Codetypescript
type ObjectDescriptor<D, M> = {
  data: D;
  methods: M & ThisType<D>;
};

function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M {
  return { ...desc.data, ...desc.methods } as D & M;
}

const obj = makeObject({
  data: { count: 0 },
  methods: {
    inc() { this.count++; },
    incTwice() { this.inc(); this.inc(); }
  }
});

What is the bug in this code?

Watch the code walkthrough

Watch on YouTube →