简体   繁体   English

我可以将参数类型指定为多种类型之一而不是 TypeScript 中的任何类型吗?

[英]Can I Specify Parameter Type as One of Many Types Instead of Any Type in TypeScript?

In a method declaration in TypeScript, the parameter could be of type array of strings, booleans, or numbers.在 TypeScript 的方法声明中,参数可以是字符串、布尔值或数字的数组类型。 Do I have to declare it as any[] or is there a way to limit the input type as on of these three types?我是否必须将其声明为 any[] 或者有没有办法将输入类型限制为这三种类型?

Typescript 1.4 introduced Union Types so the answer now is yes, you can . Typescript 1.4 引入了联合类型,所以现在的答案是肯定的,你可以

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.请注意,这与 OP 要求的不同。 These two examples have different meanings.这两个例子有不同的含义。

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:从 TypeScript 1.4 开始,似乎可以为函数参数声明多种可能的类型,如下所示:

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".这是因为“联合类型”的新 TypeScript 概念。

You can see more here .你可以在这里看到更多。

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

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;

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.另一方面,您还可以使用方法重载来为字符串、布尔值和整数数组指定不同的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM