简体   繁体   English

从 JSON 对象中选择值,其中 Typescript 类型的键

[英]Pick Values from JSON Object Where Keys in Typescript Type

I have a the following type:我有以下类型:

interface User {
  email: string;
  name: string;
}

...and the following react component: ...以及以下反应组件:

const MyComponent = <Values extends object>({ values }: { values: Values }) => {
  console.log(values);
  return <div></div>
}

Let's say I use MyComponent with the User interface, and so I'm able to pass values to it.假设我将MyComponentUser界面一起User ,因此我可以将values传递给它。 The question is: how do I modify the values JSON object being passed to pluck out any properties in it that are not present in the generic type Values ?问题是:如何修改传递给 JSON 对象的values以提取其中存在于泛型类型Values中的任何属性? In other words, if I pass the object:换句话说,如果我传递对象:

{
  email: 'mail@example.org',
  name: 'Some name',
  foo: 'bar'
}

I need the console.log(values) in the component to log the object without foo: 'bar' in it.我需要组件中的console.log(values)来记录没有foo: 'bar'的对象。

类型在运行时不存在 Typescript编译为 javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM