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);
}
}
CodePudding user response:
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
CodePudding user response:
There are a several issues with your code.
private definedOperations: Set<OperationMap>;
- has not initializer.
Possible fix: private definedOperations: Set<OperationMap> = new Set()
Type
class does not expects any constructor argument, whereas you are trying to pass two:'Integer'
andnew Set()
herenew Type('Integer', new Set());
Map
expects one iterable argument, like here:new Map([['key','value']])
. It means that if you want to create new instance ofMap
with some initial key/value pair, you should do it like here:
const operationMap: OperationMap = new Map([[' ', (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);
}
return new Value('default', Integer)
}]]);
- And the last one,
(Left: Value, Right: Value): Value
should always return aValue
, whereas you are trying to returnValue
conditionaly. If your data does not meet this reuirementsLeftNumber != NaN && RightNumber != NaN && areIntegers
you should return some defaultValue
.
Working code without errors:
const allOperationSymbols = [' ', '-', '/', '*', '^'] as const;
export type OperationSymbol = typeof allOperationSymbols[number];
class Value {
constructor(arg: string, type: Type) { }
}
type OperationMap = Map<OperationSymbol, (Left: Value, Right: Value) => Value>;
export class Type {
private definedOperations: Set<OperationMap> = new Set()
public appendOperation(operationMap: OperationMap) {
this.definedOperations.add(operationMap);
}
constructor(type: string, set: Set<unknown>) { }
}
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: OperationMap = new Map([[' ', (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);
}
return new Value('default', Integer)
}]]);
Integer.appendOperation(operationMap);
this.appendType(Integer);
}
}
P.S. Try to use Number.isNan
instead of RightNumber != NaN
. It is much safer. See docs