Abstract methods block coverage and direct calls when no subclass exists
The compiler refuses or defers abstract methods in two shapes that JavaScript handles.
Untyped lookup
A value held as any whose instances reach o[k] gets a branch per class and member. For an abstract method, the branch asks for the method's value, which an abstract declaration does not have. Every such branch is deferred with SC1090 values of abstract method declarations. The program runs and matches Node, but coverage never reports it as fully static.
abstract class Holder {
abstract current(): string;
}
class Impl extends Holder {
current(): string {
return "ok";
}
}
const read = (o: any, k: string) => o[k];
const impl: Holder = new Impl();
console.log(read(impl, "current"), impl.current());On 0.4.8, scriptc coverage of the larger form in our corpus (abstract-methods-untyped-lookup.ts) reports 6 deferred sites. The same failure reproduces on main (979a65246).
Direct call
A concrete method that calls an abstract method with no concrete implementation below the receiver's static class is refused at build time, even when the program never subclasses the class. tsc makes every instantiable subclass implement the method, so no instance can reach the call.
abstract class Source {
abstract count(): number;
liveFrom(): number {
return this.count();
}
}
class Shelf {
source: Source | null = null;
}
const shelf = new Shelf();
console.log(shelf.source === null ? "none" : shelf.source.liveFrom());Node prints none. On 0.4.8, the build fails with SC1090 calls of the abstract method 'count' with no concrete implementation below the receiver's static class.
An abstract declaration has no value at run time. An untyped lookup should select the implementation of the constructible subclass the instance belongs to, nearest first, and read undefined when no class below the receiver implements it. A direct call should evaluate its receiver and arguments, then throw Node's TypeError, <callee> is not a function.
I opened #TBD with a patch that implements both behaviors.
Activity
0No activity yet.