Can I specify a parameter type as one of many types instead of any type in TypeScript?

In a method declaration in TypeScript, a parameter can be of the type of an array of strings, booleans, or numbers. Should I declare it like any [], or is there a way to limit the input type to one of these three types?

+7
source share
6 answers

Typescript 1.4 introduced Connection Types , so the answer is now yes, you can .

function myFunc(param: string[] | boolean[] | number[]): void; 

Using a type other than those specified will result in a compile-time error.


If you need an array of several specific types, you can also use Union types for it:

 function myFunc(param: (string|boolean|number)[]): void; 

Please note that this is different from what the OP asked for. These two examples have different meanings.

+23
source

This seems like a bit of an old question, but anyway I stumbled upon it and skipped this other answer that I bring.

From TypeScript 1.4, it seems that you can declare several possible types for a function parameter as follows:

 class UtilsClass { selectDom(element: string | HTMLElement):Array<HTMLElement> { //Here will come the "magic-logic" } } 

This is due to the new TypeScript concept of "union-types".

Here you can see more.

+6
source

For this you can use function overloading:

 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 
+1
source

Not. TypeScript supports only one TypeAnnotation parameter per parameter, for example. x: string , a number or any, so you cannot specify a set of permitted types.

However, TypeScript does support function overloads (p51 documentation):

Functional overloads allow a more precise specification of call patterns supported by a function than is possible with a single signature. [...]

  function attr(name: string): string; function attr(name: string, value: string): Accessor; function attr(map: any): Accessor; function attr(nameOrMap: any, value: string): any { if (nameOrMap && typeof nameOrMap === "object") { // handle map case } else { // handle string case } } 

Otherwsie you can use typeof type checks, but this slightly increases the likelihood of a Type Script type .

 function myFunc(x){ if(typeof x !== "string" && typeof x !== "object" && typeof x !== "number") throw new TypeError("Unsupported type!"); } 
+1
source

Since strings, booleans and numbers are primitive types, I don't think there is an easy way. If you used a set of different types of objects, you could create a superclass and then specify that superclass in the interface of your method. On the other hand, you can also use method overloading to specify different implementations for arrays of strings, booleans, and integers.

0
source

Another way to resolve this is to find common methods and properties between input types and declare a string type in the declaration of the method that contains these common methods and properties. Like this:

 methodName(param1: { prop1: number; prop2: string; }, param2: { propA: bool; propB: string; } ): methodResultType; 
0
source

All Articles