相关疑难解决方法(0)

为什么我不能返回一个通用的“T”来满足 Partial<T>?

我在 TypeScript 中写了一些代码:

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

这会产生一个错误:

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

这似乎是一个错误 -{ x: 10 }显然是一个Partial<Point>. TypeScript 在这里做错了什么?我该如何解决?

generics typescript

4
推荐指数
1
解决办法
310
查看次数

标签 统计

generics ×1

typescript ×1