简体   繁体   English

如何在参数的子集上使用 javascript object 解构并保持对单个值中所有剩余属性的引用?

[英]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.在下面的示例中,我只想对属性onlyForBalsoOnlyForB使用 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 ,除了它没有onlyForBalsoOnlyForB键。

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.

相关问题 尝试创建字符串数组以用于解构 function 以删除 object 属性,只有单个字符串值有效 - Trying to create array of strings to use in a destructuring function for removing object properties, only single string value works 解构 object 以使所有属性都没有嵌套/单级 - Destructuring an object in order to make the all properties none nested / single level 如何使用对象解构扩展运算符来删除多个对象属性 - How to use object destructuring spread operator to delete multiple object properties 在TypeScript中进行对象解构时重命名剩余的属性变量 - Renaming remaining properties variable when object destructuring in TypeScript 更换javascript中的object后如何维护参考 - How to maintain reference after replacing object in javascript 如何获取具有嵌套属性的Javascript对象的子集? - How to get a subset of a Javascript object with nested properties? 如何获取 javascript 对象属性的子集 - How to get a subset of a javascript object's properties javascript中的键和对象的解构参数 - Destructuring argument for key along with object in javascript JavaScript返回的对象属性是通过引用还是返回值? - Javascript returned object properties returned by reference or value? 如何在 JavaScript ES6 中使用数组解构并分配给对象属性 - How to use array destructuring and assign to an object property in JavaScript ES6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM