从 Number 扩展的泛型类型导致 TS2362 错误

Generics type that extends from Number results in TS2362 error

我有一段代码:

function reverse<T extends Number, D extends Number>(items: T[], m: D): T[] {
    var toreturn = [];
    for (var i = items.length - 1; i >= 0; i--) {
        (()=>{
            toreturn.push(items[i] * m);
        })();
    }
    return toreturn;
}

var sample = [1, 2, 3];
var reversed = reverse(sample, 10);
console.log(reversed);

我的IDE说这里有2个错误:

Error:(5, 27) TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

Error:(5, 38) TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

基本上是关于实体不能相乘,因为它们不是数字或其他可能的类型。我已将扩展添加到通用定义中。

如何解决?

相关的 typescript playground 版本是 here

有点烦人,但由于您的约束是针对 Number 的大写字母 N,因此您必须对 Number 然后 number:

进行类型断言
(items[i] as Number as number) * (m as Number as number)

如果它适用于函数的使用方式,您可以将通用约束更改为 number 而不是 Number,这样您只需声明 number。或者也许你真的不需要泛型?在这种情况下,删除泛型并将类型 TD 替换为 number.

请注意,实际上不需要第二个通用约束,因为 m 的类型可以只是基本类型而不影响 return 类型,因此您可以通过以下方式改进它:

function reverseAndMultiplyBy<T extends number>(items: T[], m: number): T[] {
    const toreturn = [];
    for (let i = items.length - 1; i >= 0; i--) {
        toreturn.push((items[i] as number) * m);
    }
    return toreturn;
}

您可以使用类型断言告诉编译器您知道这些将是数字。

function reverse<T extends number, D extends number>(items: T[], m: D): T[] {
    var toreturn = [];
    for (var i = items.length - 1; i >= 0; i--) {
        (()=>{
            toreturn.push(<number>items[i] * <number>m);
        })();
    }
    return toreturn;
}

var sample = [1, 2, 3];
var reversed = reverse(sample, 10);
console.log(reversed);

你使用了转换或断言你看这里类型断言:

http://www.typescriptlang.org/docs/handbook/basic-types.html

摘自上面的link(basic-types.html):

类型断言有两种形式。一种是“尖括号”语法:

let someValue: any = "this is a string";

let strLength: number = (<string>someValue).length;

另一个是语法:

let someValue: any = "this is a string";

let strLength: number = (someValue as string).length;

function reverse<T extends number, D extends number>(items: T[], m: D): T[] {
    var toreturn = [];

    for (var i = items.length - 1; i >= 0; i--) {
        (()=>{
            toreturn.push(<number>items[i] * <number>m);
        })();
    }
    return toreturn;
}

var sample = [1, 2, 3];
var reversed = reverse(sample, 10);
console.log(reversed);

here


更新:

如果您不能在 extends 上使用数字,这可能会起作用:

toreturn.push( (<Number>items[i] as number) * (<Number>m as number) );

function reverse<T extends Number, D extends Number>(items: T[], m: D): T[] {
    var toreturn = [];

    for (var i = items.length - 1; i >= 0; i--) {
        (()=>{
            toreturn.push( (<Number>items[i] as number) * (<Number>m as number) );
        })();
    }
    return toreturn;
}

var sample = [1, 2, 3];
var reversed = reverse(sample, 10);
console.log(reversed);

here