在 TypeScript 中,"extends keyof" 和 "in keyof" 是什么意思?

In TypeScript, what do "extends keyof" and "in keyof" mean?

在 TypeScript 中,一些类型是使用 extends keyofin keyof 定义的。我试图理解他们的意思,但到目前为止我没有成功。

我得到的是 keyof 单独 returns 一个联合类型,它具有所有名称作为可能的值,这些名称作为 属性 名称存在于您在 keyof.

type T = keyof string;

T 因此等同于 startsWith | endsWith | trim | substring | ....

这是正确的吗?

现在,如果我试着思考 extends keyofin keyof 的意思,我的直觉是这样的:

因此,从这个 POV extends keyof 将描述一个 >= 关系,in keyof 将描述一个 <= 关系。这个对吗?如果不是,那正确的是什么?

对于任何类型 Tkeyof T 是已知的并集,public 属性 名称 T

示例:

interface Person {
  age: number;
  name: string;
}

type PersonKeys = keyof Person; // "age" | "name"

因此,您关于 keyof string 产生 startsWith | endsWith | trim | ... 的假设是正确的。您可以在 lookup type release notes.

中了解更多信息

扩展 keyof

extends,在本例中,习惯于constrain the type of a generic parameter。示例:

<T, K extends keyof T>

因此,

K 只能是 T 的 public 属性 名称。它与扩展类型或继承无关,与 extending interfaces.

相反

extends keyof 的用法如下:

function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}

const person: Person = {
  age: 22,
  name: "Tobias",
};

// name is a property of person
// --> no error
const name = getProperty(person, "name");

// gender is not a property of person
// --> error
const gender = getProperty(person, "gender");

除了 documentation on index types, I found this helpful article.

在 keyof

in 在我们定义一个 index signature 时使用,我们想要使用字符串、数字或符号文字的联合来键入。结合 keyof 我们可以使用它来创建所谓的 映射类型 ,它重新映射原始类型的所有属性。

in keyof 的用法如下:

type Optional<T> = { 
  [K in keyof T]?: T[K] 
};

const person: Optional<Person> = {
  name: "Tobias"
  // notice how I do not have to specify an age, 
  // since age's type is now mapped from 'number' to 'number?' 
  // and therefore becomes optional
};

除了 documentation on mapped types, I once again found this helpful article.

Fun fact: The Optional<T> type we've just built has the same signature as the official Partial<T> utility type!