Reputation: 28810
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
Upvotes: 0
Views: 373
Reputation: 214959
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);
}
}
Upvotes: 1