You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular/TypeScript:相似对象间属性快速赋值方法

Can I assign obj1 = obj2 to copy common properties between similar classes, or do I have to assign each property individually?

Great question! Let's break down your options based on how your Product and OffertRow classes are structured in TypeScript:

First: Why direct assignment might (or might not) work

TypeScript uses a structural type system for most cases, meaning if two types have the same public shape (properties/methods), they're considered compatible—unless your classes include private or protected members (those make types "nominal" instead of structural).

  • If both classes only have the idProduct and ProductName public properties (no extra properties, no private/protected members), then obj1 = obj2 will work out of the box:

    class Product {
      idProduct: number;
      ProductName: string;
    }
    
    class OffertRow {
      idProduct: number;
      ProductName: string;
    }
    
    let obj1: Product;
    let obj2: OffertRow = { idProduct: 123, ProductName: "Wireless Headphones" };
    obj1 = obj2; // ✅ This is valid
    
  • If either class has extra properties (e.g., Product has a price property that OffertRow doesn't), direct assignment will throw a type error because obj2 doesn't meet all requirements of Product.

Better options for copying only common properties

If direct assignment isn't feasible (due to extra properties or type safety concerns), here are reliable approaches:

1. Explicitly assign each common property

This is the most type-safe method—you know exactly what's being copied, and TypeScript will flag any typos or missing properties:

obj1.idProduct = obj2.idProduct;
obj1.ProductName = obj2.ProductName;

2. Use object spread with type narrowing

For a more concise one-liner, use the spread operator to copy only the shared properties (and preserve any existing properties on obj1):

obj1 = { ...obj1, idProduct: obj2.idProduct, ProductName: obj2.ProductName };

// If creating a new Product instance from OffertRow:
const newObj1: Product = {
  ...obj2,
  // Add any required Product properties that OffertRow lacks
  price: 79.99
};

3. Create a reusable utility function

If you need to copy these properties multiple times, wrap the logic in a function to keep your code DRY (Don't Repeat Yourself):

function copyProductCommonProps(source: OffertRow, target: Product): void {
  target.idProduct = source.idProduct;
  target.ProductName = source.ProductName;
}

// Usage:
copyProductCommonProps(obj2, obj1);

4. Use type assertion (use with caution)

If you're confident OffertRow only has the properties you need for Product, you can use a type assertion to bypass TypeScript's checks—but be warned this removes type safety (if OffertRow gains extra properties later, they'll be ignored or cause unexpected issues):

obj1 = obj2 as Product;

Final recommendation

Stick with explicit property assignment or a utility function for maximum type safety. Reserve direct assignment only when you're sure the types are fully structurally compatible, and avoid type assertions unless you have no other choice.

内容的提问来源于stack exchange,提问作者DarioN1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:18:41