Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write a Generalized function to perform OR operation in TypeScript?

In TypeScript, is there a way we can create a generalized function to execute OR operation for any number of arguments passed?

I can write a function for 2 arguments. The requirement is to generalize this for any number of arguments.

export const performOR = (arg1, arg2) => arg1 || arg2;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const isArg1OrArg2 = performOR(x, y);

If this can be achieved by passing an array of arguments, that’s also fine.

>Solution :

This sounds like homework, but:

export const performOR = (...args: any[]):boolean  => {
  for(const item of args) {
    if (item) return true;
  }
  return false;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading