如何创建打字稿水合物类型?

时间:2019-09-15 18:36:08

标签: typescript types

这个问题很可能是重复的,但是尽管之前可能有人问过,但我找不到。

我有两个对象,一个请求和一个报告。我已经看到了这种称为对象水化的现象。这是我要为其输入正确的输入/输出的示例。

loopback: 127.0.0.1
googleDNS: 8.8.8.8

这是我尝试过的:

const whatGoesIn = {
  name: true,
  age: true,
}

const whatComesOut = {
  name: "Seph",
  age: 29,
}

// I'm trying to make a function which takes a request of properties, and returns those properties filled
function report(request: typeof whatGoesIn): typeof whatComesOut {}

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:

export type Reporter<OUTLINE> = 
  (request: ReportRequest<OUTLINE>) => Pick<OUTLINE, keyof typeof request>;

在第一个REQUEST = ReportRequest<OUTLINE>中,REQUEST可能会被覆盖,因此它与OUTLINE具有相同的键并不是固有的。在后一种尝试中,我只需要添加typeof即可使请求成为类型而不是值。

相关问题