参数顺序无关紧要吗?因为TypeScript的原因吗?

9 浏览
0 Comments

参数顺序无关紧要吗?因为TypeScript的原因吗?

我想实现类似于这样的效果:

class TestClass {
    someMethod(stringParameter: string): void {
        alert("Variant #1: stringParameter = " + stringParameter);
    }
    someMethod(numberParameter: number, stringParameter: string): void {
        alert("Variant #2: numberParameter = " + numberParameter + ", stringParameter = " + stringParameter);
    }
}
var testClass = new TestClass();
testClass.someMethod("string for v#1");
testClass.someMethod(12345, "string for v#2");

这是我不想做的一个例子(我真的很讨厌JavaScript中的这种重载方法的方式):

class TestClass {
    private someMethod_Overload_string(stringParameter: string): void {
        // 可能会有很多代码...我不想把它与在通用函数中使用switch或if语句混合在一起
        alert("Variant #1: stringParameter = " + stringParameter);
    }
    private someMethod_Overload_number_string(numberParameter: number, stringParameter: string): void {
        alert("Variant #2: numberParameter = " + numberParameter + ", stringParameter = " + stringParameter);
    }
    private someMethod_Overload_string_number(stringParameter: string, numberParameter: number): void {
        alert("Variant #3: stringParameter = " + stringParameter + ", numberParameter = " + numberParameter);
    }
    public someMethod(stringParameter: string): void;
    public someMethod(numberParameter: number, stringParameter: string): void;
    public someMethod(stringParameter: string, numberParameter: number): void;
    public someMethod(): void {
        switch (arguments.length) {
        case 1:
            if(typeof arguments[0] == "string") {
                this.someMethod_Overload_string(arguments[0]);
                return;
            }
            return; // 对于这种情况来说是无法到达的区域,不必要的返回语句
        case 2:
            if ((typeof arguments[0] == "number") &&
                (typeof arguments[1] == "string")) {
                this.someMethod_Overload_number_string(arguments[0], arguments[1]);
            }
            else if ((typeof arguments[0] == "string") &&
                     (typeof arguments[1] == "number")) {
                this.someMethod_Overload_string_number(arguments[0], arguments[1]);
            }
            return; // 对于这种情况来说是无法到达的区域,不必要的返回语句
        }
    }
}
var testClass = new TestClass();
testClass.someMethod("string for v#1");
testClass.someMethod(12345, "string for v#2");
testClass.someMethod("string for v#3", 54321);

如何在TypeScript语言中实现方法重载?

0
0 Comments

Argument order does not matter? Because of TypeScript?

在 TypeScript 中,方法的重载是不支持的。这是因为 TypeScript 需要与未定义类型的 JavaScript 进行互操作。换句话说,如果你在 JavaScript 中调用重载的方法,它只能被派发到一个方法的实现上。

有一些相关的讨论在 codeplex 上。例如:

https://typescript.codeplex.com/workitem/617

我仍然认为 TypeScript 应该自动生成所有的 if 和 switch,这样我们就不需要手动处理了。

0
0 Comments

由于TypeScript试图避免不必要的运行时性能损耗,规范中明确支持方法重载,但实现起来非常笨拙,需要手动检查参数类型。我认为这主要是因为在纯JavaScript中最接近方法重载的方式也需要进行类型检查,而TypeScript试图避免修改实际的方法体。

如果我理解正确,你首先必须为每个重载编写一个方法声明,然后编写一个方法实现来检查其参数以决定调用哪个重载。实现的签名必须与所有重载兼容。

JavaScript根本不支持方法重载,对吗?那么回到JS能帮助什么呢?

对于检查接口,难道你没有比这更好的解决方案吗:stackoverflow.com/questions/14425568/…

嗯,我不太喜欢这个,只有一个可选参数会更容易阅读。

我认为重要的是它使你的代码保持可读性,而不需要大量的类型检查if语句。谁在乎它编译成什么样子呢。

根据以上内容,我们可以得出以下结论:

TypeScript实际上支持方法重载,但是实现起来比较麻烦,需要手动检查参数类型。这主要是因为在纯JavaScript中实现方法重载也需要进行类型检查,并且TypeScript试图避免不必要的运行时性能损耗。

要实现方法重载,首先需要为每个重载编写方法声明,然后编写一个方法实现来检查参数类型以确定调用哪个重载。实现的签名必须与所有重载兼容。

JavaScript本身不支持方法重载,因此回到JavaScript也无法解决这个问题。

除了手动检查参数类型外,还可以考虑使用接口进行类型检查。

但是有人认为,使用可选参数会更容易阅读,并且不需要大量的类型检查。

尽管TypeScript支持方法重载,但实现起来比较麻烦,需要进行大量的手动类型检查。这可能是因为在纯JavaScript中实现方法重载也需要进行类型检查,并且TypeScript试图避免不必要的运行时性能损耗。

0
0 Comments

Argument order does not matter in TypeScript because of the flexibility provided by optional and default parameters. This allows for more readable code and avoids creating overloads with unintuitive ordering.

The general law of TypeScript overloads states that if you can delete the overload signatures and all tests still pass, then you don't need TypeScript overloads. This means that optional and default parameters can often achieve the same results as overloads.

In the case of method overloading in TypeScript, it is advised to provide consistency in ordering. For example, if a method has a required string parameter and an optional number parameter, the string parameter should be placed first in the signature. However, following the law of TypeScript overloads, the order of the parameters can be changed without affecting the functionality of the code.

If the original order of the parameters needs to be preserved, a lot of branching and type checking is required. However, it is often more practical to have separate methods instead of trying to maintain the original order in overloads.

Overall, optional and default parameters provide a more readable and flexible alternative to method overloads in TypeScript. It is important to consider the specific use case and choose the approach that best suits the needs of the code.

0