mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-01 07:33:12 +08:00
25 lines
700 B
TypeScript
25 lines
700 B
TypeScript
// A species result of another element type is still an instance of the
|
|
// class, so `instanceof` narrows it back. Its elements are not the class's,
|
|
// so the class's fields and methods on it are refused where they are read.
|
|
class Holder {
|
|
tag = "h";
|
|
}
|
|
|
|
class Group extends Array<Holder | null> {
|
|
owner: Holder | null = null;
|
|
|
|
firstTag(): string {
|
|
const first = this[0];
|
|
return first === null || first === undefined ? "none" : first.tag;
|
|
}
|
|
}
|
|
|
|
const groups = new Group();
|
|
groups.push(new Holder(), null);
|
|
const numbers = groups.map((_, i) => i + 1);
|
|
if (numbers instanceof Group) {
|
|
numbers.owner = new Holder();
|
|
console.log(numbers.owner.tag);
|
|
console.log(numbers.firstTag());
|
|
}
|