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
Activity
georg-getz commentedon Aug 12, 2022
Hi, I randomly got here from following #2423. Wouldn't
Childbe a better name thanParentfor the extending class :DMaxGraey commentedon Aug 12, 2022
@georg-getz good point. Fixed