Javascript 我可以将参数类型指定为多种类型之一而不是 TypeScript 中的任何类型吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12776625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I Specify Parameter Type as One of Many Types Instead of Any Type in TypeScript?
提问by Amr
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?
在 TypeScript 的方法声明中,参数可以是字符串、布尔值或数字的数组类型。我是否必须将其声明为 any[] 或者有没有办法将输入类型限制为这三种类型?
回答by Joao
Typescript 1.4 introduced Union Typesso 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. These two examples have different meanings.
请注意,这与 OP 要求的不同。这两个例子有不同的含义。
回答by Blasfemizer
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.
你可以在这里看到更多。
回答by Ryan Cavanaugh
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
回答by Valentin
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.
由于字符串、布尔值和数字是原始类型,我认为没有简单的方法。如果您要使用一组不同的对象类型,您可能会想出一个超类,然后在方法的接口中指定该超类。另一方面,您还可以使用方法重载来为字符串、布尔值和整数数组指定不同的实现。
回答by Amr
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;