Typescript wrong inference when using object method

Viewed 22

I was playing around trying to make a function that creates a store, and tried something like this signature:

function makeStore<T, A extends { [key: string]: (this: T) => void }>(args: {
  state: () => T;
  actions: A;
}) {}

makeStore({
  // state: ()=>({a: 10}),
  state() {
    return { a: 0 };
  },
  actions: {
    hi() {
      this.a;
    },
  },
});

The this is correctly inferred when I use the arrow function for the state atribute, but not when I use the method style. Why is that?

1 Answers

Maybe it was a bug. Now in version 4.7 of typescript, they both work.

Related