typescript - 错误 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);
}
}
解决方案
预期 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
推荐阅读
- mysql - 如何在 MySQL 的特定日期每 3 小时获取一次聚合结果?
- rust - 无法使用 cargo uninstall 卸载 ~/.cargo 中的包:包 id 规范匹配没有包
- laravel - laravel 5.6 和 apache 为 url 提供 404
- javascript - Electron 中的调试:如何从控制台访问渲染范围对象?
- vba - 如何在不使用 VBA 的情况下对 Excel 工作表中的 CAS 编号执行数据验证?
- python - Python用字符串变量定义文件名,包括'\'
- python - node.js 中的 HTTP POST 请求之前如何做一些事情?
- python - 从 python 脚本但在 python 脚本之外运行 python 脚本
- java - 如何在不提供端口号的情况下使用域名访问tomcat应用程序
- python - Django Google Sign in ModuleNotFoundError at /auth/complete/google-oauth2/