0

If I have this:

export class JobFields implements EntityFields {} (it could also extends from EntityFields if it helps)

can I have my method accepting only references for classes that implements/extends EntityFields? Like:

private test(entity: EntityFields) {} //this does not work, it accept anything

I'm creating components that automatically reads from classes that contains information regarding back-end entities. I just want to force/induce the Developper to give it the right kind of class reference.

this.test(JobFields); //this should work
this.test(JobView);   //while this shouldn't

1 Answers1

0

You can write custom guard functions in Typescript

Here is an example. Consider the following:

IEntityFields {
    prop1: string,
    fieldprop2: {prop: string},
    metaData: {azureFilePath: string}
}

export const instanceOfEntityFields = (_o: any): _o is EntityFields => {
  return 'prop1' in _o && 'fieldprop2' in _o.metaData && 'azureFilePath' in _o.metaData;
};

then in your method calls, you could use the check...

private test(entity: EntityFields) {
    return instanceOfEntityFields(entity)
        ? //...do work here
        : false;
}

this is a cool pattern, because you can stack the checks; e.g.

export const instanceOfJobFields = (_o: any): _o is JobFields => {
      return 'jobprop1' in _o && instanceOfEntityFields(_o);
    };
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
joshvito
  • 1,498
  • 18
  • 23
  • I liked that, but this way tslint still won't say anything when I cal "test" method with something else, like a string or a number, so it won't help me – Davi Daniel Siepmann Jun 02 '20 at 16:52
  • tslint should catch calling a method with the wrong object type for input. Are you sure the IDE has tslint and/or eslint installed and enabled? Also, double check the tslint config for your project too. – joshvito Jun 02 '20 at 17:09
  • please, check this out: https://stackblitz.com/edit/typescript-biuvvg – Davi Daniel Siepmann Jun 02 '20 at 17:33
  • in your exemple it is breaking at runtime, there's no tslint telling me I gave it the wrong entity, sorry if I wasn't clear enought – Davi Daniel Siepmann Jun 02 '20 at 17:34
  • if you actually define the interface with some properties, it does tell you the types are mismatched. Looks like I can edit https://stackblitz.com/edit/typescript-biuvvg – joshvito Jun 02 '20 at 17:49
  • also, looks like this error `Error in /~/index.ts (11:76) Cannot convert undefined or null to object` is a result of your `Object.keys(null)` code – joshvito Jun 02 '20 at 17:51
  • Your comment ` //All entities are diferent, I cannot have any logic here`. If you cannot define an interface, you could try using instanceof e.g. https://stackoverflow.com/questions/24705631/typescript-myobject-instanceof – joshvito Jun 02 '20 at 17:54
  • sorry, my wedits were not saved, here is an example of both. https://stackblitz.com/edit/typescript-q6uzrb ; We can take this to chat if you want. – joshvito Jun 02 '20 at 18:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215193/discussion-between-davi-daniel-siepmann-coradini-and-joshvito). – Davi Daniel Siepmann Jun 02 '20 at 19:14