Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  12] [ 2]  / answers: 1 / hits: 88492  / 12 Years ago, mon, october 8, 2012, 12:00:00

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?


More From » typescript

 Answers
26

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.


[#82707] Friday, October 5, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrence

Total Points: 120
Total Questions: 115
Total Answers: 87

Location: England
Member since Fri, May 22, 2020
4 Years ago
terrence questions
Sat, Jun 5, 21, 00:00, 3 Years ago
Wed, Jun 17, 20, 00:00, 4 Years ago
;