首页 > 解决方案 > 错误 TS2554:预期 0-1 参数,但得到 2

问题描述

我不明白为什么编译器会给我这样的错误消息。
我试图插入一个 lambda 作为函数的参数,从而得到一个错误。在此先感谢所有帮助者!

const allOperationSymbols = ['+', '-', '/', '*', '^'] as const;
export type OperationSymbol = typeof allOperationSymbols[number];

type OperationMap = Map<OperationSymbol, (Left: Value, Right: Value) => Value>;
export class Type {

    private definedOperations: Set<OperationMap>;

     public appendOperation(operationMap: OperationMap) {
        this.definedOperations.add(operationMap);
    }
}
class Types {
    private value: Array<Type> = [];

    public appendType(type: Type) {
        this.value.push(type);
    }

    public addInteger() {
        let Integer: Type;
        Integer = new Type('Integer', new Set());

        const operationMap = new Map('+', /*here is the error*/ (Left: Value, Right: Value): Value => {
        const LeftNumber = Number(Left);
        const RightNumber = Number(Right);
        const areIntegers = Number.isInteger(LeftNumber) && Number.isInteger(RightNumber);
        if (LeftNumber != NaN && RightNumber != NaN && areIntegers) {
            return new Value((Number(Left) + Number(Right)).toString(), Integer);
        }
    });

    Integer.appendOperation(operationMap);

    this.appendType(Integer);
    }
}

标签: typescript

解决方案


预期 0-1 个参数,但得到 2 个

您使用了Map错误的构造函数(并且 TypeScript 指出了这一点)。

你的用法:

new Map('+', function);

JavaScript 中的Map构造函数不接受元组(0 个参数)或一个数组(1 个参数)key,value。例如正确用法(1 个参数版本):

 new Map([
   ['+', function]
 ]);

更多的

请参阅:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/Map


推荐阅读