Why can’t I return a generic ‘T’ to satisfy a Partial?

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> {

Leave a Comment