Issue
I don't get why the compiler five me such an error message.
I am trying to insert a lambda as an argument to a function and thereby get an error.
Thanks in advance to all the helpers!
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);
}
}
Solution
Expected 0-1 arguments, but got 2
You are using the Map
constructor wrong (and TypeScript is pointing it out).
Your usage:
new Map('+', function);
The Map
constructor in JavaScript takes nothing (0 arguments) or an array (1 argument) of key,value
tuples. e.g. correct usage (1 argument version):
new Map([
['+', function]
]);
More
See : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/Map
Answered By - basarat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.