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?
When thinking about writing a generic function, there's an important rule to remember
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> {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With