I have a function, in this case I'm using XLSX.read
this function is defined to take one argument, and a second which may be undef (which is what I want).
Yet, when I try to use this function pointfree in an Rxjs Observable, like this
.pipe(
.map(XLSX.read)
)
I get an error.
error TS2345: Argument of type '(data: any, opts?: ParsingOptions | undefined) => WorkBook' is not assignable to parameter of type '(value: any, index: number) => WorkBook'.
Types of parameters 'opts' and 'index' are incompatible.
Type 'number' is not assignable to type 'ParsingOptions | undefined'.
Instead what TypeScript wants is,
.pipe(
.map( (x:any) => XLSX.read(x) )
),
This is because Observable.operator.map is defined as
map<T, R>(project: (value: T, index: number) => R, thisArg?: any): OperatorFunction<T, R>
Why is that map is (project: (value: T, index: number)
and not (project: (value: T, index?: number)
with an optional index
argument which TypeScript seems to support,
In JavaScript, every parameter is optional, and users may leave them off as they see fit. When they do, their value is undefined. We can get this functionality in TypeScript by adding a
?
to the end of parameters we want to be optional.