mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-28 06:53:42 +08:00
A package function that destructures its options object, such as
makeClass({ Base, name }), now specializes per call site like positional
parameters: each destructured name binds the static type of the matching
property, so a class passed as { Base } keeps its static type and
class extends Base builds a constructible subclass.
Fixes #938
Co-authored-by: phall <25888173+phall1@users.noreply.github.com>
16 lines
661 B
TypeScript
16 lines
661 B
TypeScript
// A package factory that receives its base class through a destructured
|
|
// options object builds subclasses of program classes that construct and
|
|
// dispatch like Node.
|
|
import { Class, extend } from "class-options/classes.js";
|
|
|
|
class Animal extends Class("Animal") {}
|
|
class Dog extends extend(Animal, "Dog") {}
|
|
class Puppy extends extend(Dog, "Puppy") {}
|
|
|
|
const rex = new Dog("Rex");
|
|
const ada = new Animal("Ada");
|
|
const bit = new Puppy("Bit");
|
|
console.log(rex instanceof Dog, rex instanceof Animal, ada instanceof Dog, bit instanceof Animal);
|
|
console.log(rex.describe(), ada.describe(), bit.describe());
|
|
console.log(rex.kind, rex.value, ada.kind, bit.label);
|