assemblyscript
assemblyscript copied to clipboard
Add Pedantic warnings for explicit downcasting (type refinement)
Downcasting is not cheap and may produce app termination. Much better to use explicit type narrowing + implicit downcasting like:
class Animal {}
class Tiger extends Animal {}
function foo(base: Animal): void {
if (base instanceof Tiger) {
let tiger = base; // implicitly infer as base: Tiger
// use tiger
}
}
instead of
function foo(base: Animal): void {
let tiger = <Tiger>base; // may throw "unexpected downcast"
}
Pedantic mode (enabled by --pedantic
) should disallow last explicit variant and propose first one.
Depends on #2423 and #2352
Hi, I randomly got here from following #2423. Wouldn't Child
be a better name than Parent
for the extending class :D
@georg-getz good point. Fixed