Object distribution is widely used to update an old object in a functional style, for example:
let o1 = { x: 5, y: 6 };
let o2 = { ...o1, y: 10 };
alert(JSON.stringify(o2));
The new TypeScript 2.1 now implements an object extension, but unlike a simple object literal, it allows you to have unknown properties:
interface Point { x: number, y: number }
let o1: Point = { x: 5, y: 6 };
let o2: Point = { x: 5, y: 6, z: 10 };
let o3: Point = { ...o1, z: 10 };
alert(JSON.stringify(o3));
So here is the question: how to safely use a distribution object in TypeScript to update an object? Or a more general question: how to update an object in a functional (immutable) and safe form in TypeScript 2.1?
source
share