12

I wrote some code in TypeScript:

type Point = {
  x: number;
  y: number;
};
function getThing<T extends Point>(p: T): Partial<T> {
  // More interesting code elided
  return { x: 10 };
}

This produces an error:

Type '{ x: 10; }' is not assignable to type 'Partial<T>'

This seems like a bug - { x: 10 } is clearly a Partial<Point>. What's TypeScript doing wrong here? How do I fix this?

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235

1 Answers1

16

When thinking about writing a generic function, there's an important rule to remember

The Caller Chooses the Type Parameter

The contract you've provided for getThing ...

function getThing<T extends Point>(p: T): Partial<T>

... implies legal invocations like this one, where T is a subtype of Point:

const p: Partial<Point3D> = getThing<Point3D>({x: 1, y: 2, z: 3});

Of course, { x: 10 } is a legal Partial<Point3D>.

But the ability to subtype doesn't just apply to adding additional properties -- subtyping can include choosing a more restricted set of the domain of the properties themselves. You might have a type like this:

type UnitPoint = { x: 0 | 1, y: 0 | 1 };

Now when you write

const p: UnitPoint = getThing<UnitPoint>({ x: 0, y: 1});

p.x has the value 10, which is not a legal UnitPoint.

If you find yourself in a situation like this, odds are good that your return type is not actually generic. A more accurate function signature would be

function getThing<T extends Point>(p: T): Partial<Point> {
Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
  • Why would someone prefer the generic signature `function getThing(p: T): Partial;` to the concrete `function getThing(p: Point): Partial;`? Excess property checks on literals? – jcalz Oct 27 '17 at 17:57
  • 2
    You might write something like `(x: T, y: T)`. The single-arg version doesn't make much sense but sometimes it's hard to convince people they don't really need generics – Ryan Cavanaugh Oct 27 '17 at 18:01
  • We should acknowledge the inconsistency around code like the following not triggering an error: `function getThing(p: T): Partial { const ret: Partial = {}; ret.x = 10; return ret; }`. TS isn't intended to be all-soundness-all-the-time, so stuff like this is inevitable, but it might be surprising. – jcalz Oct 26 '20 at 20:47
  • [simple Stackblitz example](https://www.typescriptlang.org/play?ts=4.5.5#code/MYGwhgzhAEAKYCcCmA7ALgHgCrSQDzVQBMYB7AIwCslg0A+aAbwF8BYAKAEt0kEAzMMCTQAsqSJIQTaGk5oQSAFzQIaBNwDm0ZgG4OoSDBEBPAMpowhbLgLEj4yQ3yEUJOIlSYsDRh2j-oAHoAKmC-AOhg6GBSFFVockRleARZMBBsBixjAAdhAHJGGTkFZQAiMHJgMp1tfOhOGBRSNGhDTg0USoUZUhlcgpS0jO98gDoACgAmAGYpqYBKcICoiCQlaAALNDQciEVAwNVBAGtSADdePhBSAHcxmIBbQIBHAFckVU5YiECAFgAbABOAAcAAYAOwAmaBW6bYwAWmAYHQCM4COQaDeCBQCLACI0qF4nGACLQZNICIglkafER+JyiGGaGW-mCgVZvAQySZnHSmWgAF4mKyIrJ5Bt8pVgPlWWx2BFSCceak+RkxBIQAxhb4FREAuLStB8kqwMZZXr-PKOMwgA) – TmTron Mar 21 '22 at 08:23