Skip to content

Commit 84c2e95

Browse files
authored
setting recordNode value to undefined behaves like delete (#25739)
## Description This PR updates the set function to behave like delete when record node values are set "= undefined". ## Breaking Changes None
1 parent b2efe29 commit 84c2e95

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

packages/dds/tree/src/simple-tree/node-kinds/record/recordNodeTypes.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ import type { RestrictiveStringRecord } from "../../../util/index.js";
2626
* Therefore code assigning to these fields must explicitly construct nodes using the schema's constructor or create method,
2727
* or using some other method like {@link (TreeAlpha:interface).create}.
2828
*
29+
* Field Assignment and Deletion
30+
*
31+
* Unlike JavaScript's `Record` type, assigning `undefined` to a key in {@link TreeRecordNode} behaves like a `delete` operation,
32+
* removing the field's contents.
33+
*
34+
* This is to stay consistent with {@link TreeObjectNode} behavior.
35+
*
36+
* Example:
37+
* ```ts
38+
* const Numbers = schemaFactory.record("Numbers", schemaFactory.number);
39+
* const record = new Numbers({ a:1, b:2 });
40+
*
41+
* // This is equivalent to delete record.a, and removes the entry.
42+
* record.a = undefined
43+
* ```
44+
*
2945
* @beta
3046
*/
3147
export interface TreeRecordNode<

packages/dds/tree/src/test/simple-tree/node-kinds/recordNode.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ describe("RecordNode", () => {
230230
assert.equal(record.baz, 4);
231231
});
232232

233+
it("setting value to undefined behaves as a delete", () => {
234+
const record = init(schemaType, { foo: 1 });
235+
assert.equal(record.foo, 1);
236+
(record as Record<string, number | undefined>).foo = undefined;
237+
assert.equal(record.foo, undefined);
238+
});
239+
233240
it("can delete values", () => {
234241
const record = init(schemaType, { foo: 1, bar: 2 });
235242
assert.equal(record.foo, 1);

0 commit comments

Comments
 (0)