dimanche 18 juin 2017

Is there a Typescript equivalant to Java/.NET Reflection? OR - How can I get a list classes extending a given abstract class?

Call me Noah.

// AnimalsBase: a package installed via 'npm install animals-base'
export default abstract class Animal {...}

There's this abstract base class, Animal, defined in an npm package, which gets extended by other packages:

// earth - I know about it because of 'npm install animals-earth'
import Animal from 'creatures-base'

export class Human extends Animal {...}
export class Cat extends Animal {...}
export class Dog extends Animal {...}
...

As you may know, I need to gather up collections that include every type of animal at runtime. Thing is, I'm never sure what planet I'll be working on any given day:

// tatooine - installed via 'npm install animals-tatooine'
import Animal from 'creatures-base'

export class WampRat extends Animal {...}
export class Bantha extends Animal {...}
export class SandPerson extends Animal {...}
...

I might even be expected to work several different planets on a given shift (deployment)...

Question One:

Can this function be written?

function animalCollection() : [Animal] {
    return [ /* An array containing one of each type of animal */ ]
}

Question Two:

If the above is possible, can I go further and discern the home planets of each animal?

function animalCollection(planet) : [Animal] {
    return [ /* An array containing one of each type of animal native to 'planet' */ ]
}





Aucun commentaire:

Enregistrer un commentaire