Categories
TypeScript Answers

How to specify parameter type as one of many types instead of any type in TypeScript?

Spread the love

Sometimes, we want to specify parameter type as one of many types instead of any type in TypeScript.

In this article, we’ll look at how to specify parameter type as one of many types instead of any type in TypeScript.

How to specify parameter type as one of many types instead of any type in TypeScript?

To specify parameter type as one of many types instead of any type in TypeScript, we can create a union type.

For instance, we write

const myFunc = (param: string[] | boolean[] | number[]): void => {
  //...
};

to create the myFunc function that takes the param parameter which can either be a string arrat, number array or boolean array.

And the return type is void so it returns nothing.

Conclusion

To specify parameter type as one of many types instead of any type in TypeScript, we can create a union type.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *