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>
32 lines
923 B
JavaScript
32 lines
923 B
JavaScript
// A class factory that takes its base and settings as one options object,
|
|
// as Effect's makeClass does: `makeClass({ Base, name })` destructures them
|
|
// and builds `class extends Base`.
|
|
function makeClass({ Base, name, tag: label }) {
|
|
return class extends Base {
|
|
constructor(value) {
|
|
super(value);
|
|
this.kind = name;
|
|
this.label = label;
|
|
}
|
|
describe() {
|
|
return `${this.label}:${this.kind}:${this.value}`;
|
|
}
|
|
// Each class passes itself back as a base: a call site must not
|
|
// specialize makeClass on the classes makeClass itself defines.
|
|
static extend(name) {
|
|
return makeClass({ Base: this, name, tag: label });
|
|
}
|
|
};
|
|
}
|
|
class Root {
|
|
constructor(value) {
|
|
this.value = value;
|
|
}
|
|
}
|
|
export function Class(name) {
|
|
return makeClass({ Base: Root, name, tag: "root" });
|
|
}
|
|
export function extend(Base, name) {
|
|
return makeClass({ Base, name, tag: "sub" });
|
|
}
|