[英]How to use javascript object destructuring on a subset of an argument and maintain reference to all remaining properties in single value?
I'm trying to figure out if there's a way to use typescript/javascript object destructuring to target only a subset of the objects properties while maintaining the remaining properties of the object in its own variable, "catching all other properties/keys".我试图弄清楚是否有一种方法可以使用 typescript/javascript object 解构以仅针对对象属性的子集,同时将 object 的其余属性保留在其自己的变量中,“捕获所有其他属性/键”。
My use case is related to using inheritance to extending a class while attempting to leave the api's the similar.我的用例与使用 inheritance 扩展 class 相关,同时尝试保留 API 的相似性。 In the example below I only want to use object destruction for properties
onlyForB
and alsoOnlyForB
while pass all remaining properties to config
variable.在下面的示例中,我只想对属性
onlyForB
和alsoOnlyForB
使用 object 破坏,同时将所有剩余属性传递给config
变量。
class A {
constructor(config) {
this.name = config.name;
// ...
}
}
class B extends A {
constructor({ onlyForB, alsoOnlyForB, ..config }) { // using ...config doesn't work
this.onlyForB = onlyForB;
this.alsoOnlyForB = alsoOnlyForB;
super(config);
}
}
const b = new B({ onlyForB: "B", alsoOnlyForB: "B2", name: "Satoshi", age: 100});
/**
Trying to achieve the following in class B's constructor
onlyForB -> "B"
alsoOnlyForB -> "B2"
config -> { name: "Satoshi", age: 100 }
*/
When I try this with { onlyForB, alsoOnlyForB, ..config }
which is similar to creating objects with the spread syntax I get errors.当我尝试使用
{ onlyForB, alsoOnlyForB, ..config }
,这类似于使用扩展语法创建对象时出现错误。 In the real use case I am extending an open source class that has mainly "config" properties and using typescript.在实际用例中,我正在扩展一个主要具有“配置”属性并使用 typescript 的开源 class。
Is there a way to achieve this without manually deleting all the object's properties specific to the B
class?有没有办法在不手动删除特定于
B
class 的所有对象属性的情况下实现这一点?
Side Note: For those familiar with python I am trying to achieve something similar to what **kwargs
does, ie.旁注:对于熟悉 python 的人,我正在尝试实现类似于
**kwargs
所做的事情,即。 def __init__(self, onlyForB, alsoOnlyForB, **kwargs)
. def __init__(self, onlyForB, alsoOnlyForB, **kwargs)
。
There are lots of possible ways to do something like this;有很多可能的方法来做这样的事情; here is one example:
这是一个例子:
type ConfigForB = {
onlyForB: string,
alsoOnlyForB: string,
age: number,
name: string
}
type ConfigForA = Omit<ConfigForB, 'onlyForB' | 'alsoOnlyForB'>
class A {
private name: string
constructor(config: ConfigForA) {
this.name = config.name;
// ...
}
}
class B extends A {
private onlyForB: string
private alsoOnlyForB: string
constructor({ onlyForB, alsoOnlyForB, ...config }: ConfigForB) {
super(config);
this.onlyForB = onlyForB;
this.alsoOnlyForB = alsoOnlyForB;
}
}
const b = new B({ onlyForB: "B", alsoOnlyForB: "B2", name: "Satoshi", age: 100});
I've defined a ConfigForB
type that has the four properties listed in your example, and used that type in the constructor for B
.我已经定义了一个
ConfigForB
类型,它具有您的示例中列出的四个属性,并在B
的构造函数中使用了该类型。 Then I've used the Omit<Type, Keys>
utility type to construct a new ConfigForA
type, which is exactly the same as ConfigForB
, except it's without the onlyForB
and alsoOnlyForB
keys.然后我使用了
Omit<Type, Keys>
实用程序类型来构造一个新的ConfigForA
类型,它与ConfigForB
,除了它没有onlyForB
和alsoOnlyForB
键。
If I were to define this explicitly, it would look like this:如果我要明确定义它,它将如下所示:
type ConfigForA = {
age: number,
name: string
}
This would also work, by the way - using Omit
is just nice since you don't have to repeat yourself.顺便说一句,这也可以使用 - 使用
Omit
很好,因为您不必重复自己。
You can try this out on TS Playground here .您可以在此处的 TS Playground 上尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.