Angular/TypeScript:相似对象间属性快速赋值方法
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
idProductandProductNamepublic properties (no extra properties, no private/protected members), thenobj1 = obj2will 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 validIf either class has extra properties (e.g.,
Producthas apriceproperty thatOffertRowdoesn't), direct assignment will throw a type error becauseobj2doesn't meet all requirements ofProduct.
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

