In a method declaration in TypeScript, the parameter could be of type array of strings, booleans, or numbers. Do I have to declare it as any[] or is there a way to limit the input type as on of these three types?
5 Answers
Typescript 1.4 introduced Union Types so the answer now is yes, you can.
function myFunc(param: string[] | boolean[] | number[]): void;
Using other type than the ones specified will trigger a compile-time error.
If you want an array of multiple specific types, you can use Union Types for that as well:
function myFunc(param: (string|boolean|number)[]): void;
Note that this is different from what OP asked for. These two examples have different meanings.

- 11,391
- 14
- 81
- 114

- 7,366
- 4
- 32
- 48
-
To me, that looks like it can be an array consisting entirely of one type, where that type could be string, boolean or number. What about an array that can contain a mixture of only those three types? – Montgomery 'monty' Jones May 06 '16 at 14:10
-
1I'm not sure that's what the OP asked for but I edited my answer anyway. You obviously understand there's a difference between the two. – Joao May 06 '16 at 14:30
-
what if we ant to pass type other then string/boolean/number? is it possible now as i am trying to use myFunc(param: string| Reactelement) but it is giving error – Muneem Habib Apr 15 '20 at 09:31
-
Here is functional way of doing it https://stackoverflow.com/a/64794158/1948585 – user1948585 Nov 12 '20 at 03:30
This seems a bit old question, but anyway, I came across it, and missed this other answer that I bring.
From TypeScript 1.4 seems that it is possible to declare multiple possible types for a function parameter like this:
class UtilsClass {
selectDom(element: string | HTMLElement):Array<HTMLElement> {
//Here will come the "magic-logic"
}
}
This is because of the new TypeScript concept of "union-types".
You can see more here.

- 11,089
- 11
- 50
- 71

- 156
- 1
- 9
You can use function overloads to do this:
class Thing {
public foo(x: number[]);
public foo(x: bool[]);
public foo(x: string[]);
public foo(x: any[]) {
// Note: You'll have to do type checking on 'x' manually
// here if you want differing behavior based on type
}
}
// Later...
var t = new Thing();
t.foo(someArray); // Note: External callers will not see the any[] signature

- 209,514
- 56
- 272
- 235
Another way to resolve this is to find the common methods and properties between the input types and declare an in-line type in the method declaration that holds these common methos and properties. Like this:
methodName(param1: { prop1: number; prop2: string; }, param2: { propA: bool; propB: string; } ): methodResultType;

- 1,935
- 2
- 19
- 29
Since strings, booleans and numbers are primitive types I don't think there is a simple way. If you would use a set of different object types, you could maybe come up with a super class and then specify that super class in the interface of your method. On the other hand, you could also use method overloading to specify different implementations for arrays of strings, booleans and integers.

- 7,874
- 5
- 33
- 38
-
Super class could work, an in-line type that holds common methods and properties. – Amr Oct 08 '12 at 07:21