如何在打字稿中组合对象属性?

Waw*_*her 51 typescript

我想知道最好的方法,比方说我有两个对象

var objectA = {
    propertyA: 1,
    propertyB: 2
    ...
    propertyM: 13
}

var objectB = {
    propertyN: 14,
    propertyO: 15
    ...
    propertyZ: 26
}
Run Code Online (Sandbox Code Playgroud)

如果objectC是由创建的

var objectC = Object.assign(objectA, objectB);
Run Code Online (Sandbox Code Playgroud)

如何声明/描述objectC,因此编译器/ IDE知道它具有objectA和objectB的属性?

我想找到一种方法,而无需为objectA和objectB定义接口.我不想两次为同一属性编写声明和定义/评估.如果我在对象上有太多属性,则此冗余非常重要.

(是否有可以提取现有对象的接口/类型的运算符?)

可能吗?

Alk*_*sai 89

似乎这应该做的伎俩:

var objectA = {
    propertyA: 1,
    propertyB: 2,
    .
    . // more properties here
    .
    propertyM: 13
};

var objectB = {
    propertyN: 14,
    propertyO: 15,
    .
    . // more properties here
    .
    propertyZ: 26
};

var objectC = {...objectA, ...objectB}; // this is the answer

var a = objectC.propertyA;

var n = objectC.propertyN;
Run Code Online (Sandbox Code Playgroud)

基于这篇文章:https://blog.mariusschulz.com/2016/12/23/typescript-2-1-object-rest-and-spread


另外,分解中变量的顺序很重要.考虑以下:

var objectA = {
    propertyA: 1,
    propertyB: 2, // same property exists in objectB
    propertyC: 3
};

var objectB = {
    propertyX: 'a',
    propertyB: 'b', // same property exists in objectA
    propertyZ: 'c'
};

// objectB will override existing properties, with the same name,
// from the decomposition of objectA
var objectC = {...objectA, ...objectB}; 

// result: 'b'
console.log(objectC.propertyB); 

// objectA will override existing properties, with the same name,
// from the decomposition of objectB
var objectD = {...objectB, ...objectA}; 

// result: '2'
console.log(objectD.propertyB); 
Run Code Online (Sandbox Code Playgroud)

  • 只是为了改善答案:这行就可以了:var objectC = {... objectA,... objectB}; (2认同)

小智 12

尝试这个..

const person = { name: 'TRilok', gender: 'Male' };
const tools = { computer: 'Mac', editor: 'Atom' };
const attributes = { handsomeness: 'Extreme', hair: 'Brown', eyes: 'Blue' };

const summary = {...person, ...tools, ...attributes};
Run Code Online (Sandbox Code Playgroud)


bas*_*rat 8

所以编译器/ IDE知道它具有objectA和objectB的属性?

使用交集类型+泛型.比如从这里开始

/**
 * Quick and dirty shallow extend
 */
export function extend<A>(a: A): A;
export function extend<A, B>(a: A, b: B): A & B;
export function extend<A, B, C>(a: A, b: B, c: C): A & B & C;
export function extend<A, B, C, D>(a: A, b: B, c: C, d: D): A & B & C & D;
export function extend(...args: any[]): any {
    const newObj = {};
    for (const obj of args) {
        for (const key in obj) {
            //copy all the fields
            newObj[key] = obj[key];
        }
    }
    return newObj;
};
Run Code Online (Sandbox Code Playgroud)

更多

两者都在这里提到:https://basarat.gitbooks.io/typescript/content/docs/types/type-system.html


Hav*_*Hav 7

(是否有可以提取现有对象的接口/类型的运算符?是否可能?)

你应该去typeof

type typeA = typeof objectA;
type typeB = typeof objectB;
Run Code Online (Sandbox Code Playgroud)

要合并它们,您可以使用 basarat 已经指出的交集操作。

type typeC = typeA & typeB
Run Code Online (Sandbox Code Playgroud)


Eug*_*nio 6

使用Typescript 展开运算符将其转换为 Javascript Object.assign()

如果您需要深度树对象合并,您可以使用最佳全局包的更改功能