Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

typescript - Why can't I return a generic 'T' to satisfy a Partial<T>?

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...