0

I am seeing this error a hell of a lot after upgrading to typescript 3.5.

could be instantiated with a different subtype of constraint '{}' typescript error

I have no idea what it means and it is always referring to the empty {} type.

If you look at this playground on line 34, I have no idea what could be instantiated with a different subtype or what even the subtype could be.

This is a typescript generated message and I think that {} is the default type added to the error message.

This github issue exists but I'm still struggling to understand it

1 Answer 1

1

I don't understand what the rest of the code does, but the problem at hand boils down to this:

function map2<A, B>(x: A|undefined, fn: (a: A) => B): B|undefined  {
  if (x === undefined) {
      return x;
  } else {
      return fn(x);
  }
}

This doesn't check, because A can also include undefined and TS cannot decide whether to return A or undefined in the first branch.

This can be easily fixed by telling TS that A is never undefined:

function map2<A extends {}, B>(x: A|undefined, fn: (a: A) => B): B|undefined  {
  if (x === undefined) {
      return x;
  } else {
      return fn(x);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.