Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 验证传递给方法/函数的参数数_Javascript_Typescript - Fatal编程技术网

Javascript 验证传递给方法/函数的参数数

Javascript 验证传递给方法/函数的参数数,javascript,typescript,Javascript,Typescript,我知道在JavaScript中,进行重载的最佳方法是使用参数对象,如下所示: function myFunciton(arg1, arg2, arg3){ switch(arguments.length) { case 3: // Do something with 3 args break; case 2: // Do something with 2 args

我知道在JavaScript中,进行重载的最佳方法是使用
参数
对象,如下所示:

function myFunciton(arg1, arg2, arg3){
    switch(arguments.length) {
        case 3:
            // Do something with 3 args
            break;
        case 2:
            // Do something with 2 args
            break;
        case 1:
            // Do something with 1 args
            break;
        default:
            // Do something with no args
            break;
    }
}
interface MyObject {
  myFunciton: (arg1: string) => this
  myFunciton: (arg1: string, arg2: number, arg3: number) => this
}
在上面的示例中,假设您需要传递
1个参数
所有三个参数
,并且两个参数无效。您将如何在TypeScript中定义它,以便在尝试使用两个参数时抛出错误

我试着制作这样的界面:

function myFunciton(arg1, arg2, arg3){
    switch(arguments.length) {
        case 3:
            // Do something with 3 args
            break;
        case 2:
            // Do something with 2 args
            break;
        case 1:
            // Do something with 1 args
            break;
        default:
            // Do something with no args
            break;
    }
}
interface MyObject {
  myFunciton: (arg1: string) => this
  myFunciton: (arg1: string, arg2: number, arg3: number) => this
}
但是,这会产生以下错误:

重复标识符“myFunction”

在TypeScript中,通过提供多个函数类型来定义

您的函数将写为:

function myFunction(arg1);
function myFunction(arg1, arg2, arg3);
function myFunction(arg1, arg2?, arg3?) {
    switch(arguments.length) {
        case 3:
            // Do something with 3 args
            break;
        case 2:
            // Do something with 2 args (TS won't allow this one)
            break;
        case 1:
            // Do something with 1 args
            break;
        default:
            // Do something with no args (TS won't allow this one)
            break;
    }
}
interface MyObject {
    myFunction(arg1: string): this;
    myFunction(arg1: string, arg2: number, arg3: number): this;
}
您的界面将编写为:

function myFunction(arg1);
function myFunction(arg1, arg2, arg3);
function myFunction(arg1, arg2?, arg3?) {
    switch(arguments.length) {
        case 3:
            // Do something with 3 args
            break;
        case 2:
            // Do something with 2 args (TS won't allow this one)
            break;
        case 1:
            // Do something with 1 args
            break;
        default:
            // Do something with no args (TS won't allow this one)
            break;
    }
}
interface MyObject {
    myFunction(arg1: string): this;
    myFunction(arg1: string, arg2: number, arg3: number): this;
}
在TypeScript中,通过提供多个函数类型来定义

您的函数将写为:

function myFunction(arg1);
function myFunction(arg1, arg2, arg3);
function myFunction(arg1, arg2?, arg3?) {
    switch(arguments.length) {
        case 3:
            // Do something with 3 args
            break;
        case 2:
            // Do something with 2 args (TS won't allow this one)
            break;
        case 1:
            // Do something with 1 args
            break;
        default:
            // Do something with no args (TS won't allow this one)
            break;
    }
}
interface MyObject {
    myFunction(arg1: string): this;
    myFunction(arg1: string, arg2: number, arg3: number): this;
}
您的界面将编写为:

function myFunction(arg1);
function myFunction(arg1, arg2, arg3);
function myFunction(arg1, arg2?, arg3?) {
    switch(arguments.length) {
        case 3:
            // Do something with 3 args
            break;
        case 2:
            // Do something with 2 args (TS won't allow this one)
            break;
        case 1:
            // Do something with 1 args
            break;
        default:
            // Do something with no args (TS won't allow this one)
            break;
    }
}
interface MyObject {
    myFunction(arg1: string): this;
    myFunction(arg1: string, arg2: number, arg3: number): this;
}

使用新的es2015 spread语法,您可以将参数转换为数组,然后检查长度?是的,我本来打算这样做,但如何定义可能的重载?基本上,我要寻找的是预处理器解决方案,因此当TS编译时,如果使用1个参数,它会抛出错误(如上面的示例所示)或者ide可能会在它下面加下划线,说您不能使用1个参数为什么不检查函数体中的参数?使用新的es2015 spread语法,您可以将参数转换为数组,然后检查长度?是的,我正打算这么做,但是我该如何定义可能的重载呢?基本上我正在寻找的是一个预处理器解决方案,所以当TS编译时,如果您使用1个参数(如上面的示例所示),它会抛出错误,或者ide可能会在它下面加下划线,说您不能使用1个参数,为什么不检查函数体中的参数呢?这就是我所寻找的!我刚发现在你发帖子之前:)ThanksLooks好像我根本不需要使用界面,它会自动地把它捡起来这就是我要找的!我刚刚发现,在你发布之前:)ThanksLooks好像我根本不需要使用界面,它会自动拾取它