Files
scriptc/tests/corpus/5184-interface-property-objects.ts
T
Chris Tate 022ac6eba9 Store arrays of interface values natively and read interface fields directly
Arrays whose elements are class-backed interface values are native arrays
of the element capsules, so iteration, indexing and most array methods no
longer go through the checked-dynamic array, and method calls receive a
receiver they can dispatch. Methods without a native form for these
elements run on the array's live dynamic view.

A read of an interface property signature has the property's static type
and reads the field of an implementing class directly. Accessors, object
literals and JavaScript classes keep the checked property read.
2026-10-14 17:38:02 -05:00

54 lines
1.3 KiB
TypeScript

// Property reads through interfaces that return class instances, optional
// values and nested interface values: reads return the stored objects, and
// a read after a write sees the new value.
class Owner {
name: string;
constructor(name: string) {
this.name = name;
}
}
interface Node {
value: number;
owner: Owner;
next: Node | null;
}
class ListNode implements Node {
value: number;
owner: Owner;
next: Node | null = null;
constructor(value: number, owner: Owner) {
this.value = value;
this.owner = owner;
}
}
class Sentinel implements Node {
value = -1;
owner = new Owner("sentinel");
next: Node | null = null;
}
const ann = new Owner("ann");
const head: Node = new ListNode(1, ann);
const second = new ListNode(2, ann);
head.next = second;
second.next = new Sentinel();
console.log(head.owner === ann, head.owner.name, head.next === second);
let total = 0;
let names = "";
for (let node: Node | null = head; node !== null; node = node.next) {
total += node.value;
names += node.owner.name + ";";
}
console.log(total, names);
head.owner = new Owner("bob");
console.log(head.owner.name, second.owner.name);
const literal: Node = { value: 5, owner: ann, next: head };
const linked = literal.next;
console.log(literal.owner === ann, linked === head, linked === null ? -1 : linked.value);