将类型指定为与新 属性 合并的接口?

Specify type as interface merged with a new property?

在 TypeScript 中,是否可以通过某种方式执行如下操作:

interface IPerson {
  name: string;
}

interface Test {
  something: number;
  personWithVersion: IPerson & (version: string); // is something like that possible?
}

personWithVersion 应具有 IPerson 和另一个 属性 version 的属性。我可以在不创建新接口的情况下以某种方式指定它,还是必须创建一个新接口,例如 IPersonWithVersion,然后扩展 IPerson?

试试这个:

interface IPerson {
  name: string;
}

interface Test {
  something: number;
  personWithVersion: IPerson & { version: string };
}