如何使对象中的 属性 成为类型

How to make a property inside an object a type

use typescript

我想留下下面的before type,单独定义after type

type before = {
  title: string;
  sncFileInfoList: {
    sncFileKey1: string;
    sncFileKey2: string;
    sncFileKey3: string;
    sncFileKey4: string;
    sncFileNm: string;
  }[];
}
type after = {
  sncFileKey1: string;
  sncFileKey2: string;
  sncFileKey3: string;
  sncFileKey4: string;
  sncFileNm: string;
}

也就是我想用before type的sncFileInfoList属性.
重要的是 before type 应该静止不动。 我已经尝试了几种我知道的实用程序类型,但无济于事。 有一种方法可以将类型disassemble并assemble为Intersection Type,但我认为会有更优雅的方法。

如有任何帮助,我们将不胜感激‍♂️

您可以使用 Indexed Access Types 在另一种类型上查找特定的 属性:

type before = {
  title: string;
  sncFileInfoList: {
    sncFileKey1: string;
    sncFileKey2: string;
    sncFileKey3: string;
    sncFileKey4: string;
    sncFileNm: string;
  }[];
}

type after = before['sncFileInfoList'][0]

TypeScript Playground