I am trying to use rxjs 5 to write a Node.js server in TypeScript, but I hit an error when converting fs.readFile
to its rxjs form. I expect the following code
bindCallback
and bindNodeCallback
can be tricky with TypeScript, as it all depends upon how TypeScript infers the function parameters.
There is likely a better way, but this is what I do to see exactly what is being inferred: assign the observable to something totally incompatible and look closely at the effected error. For example, this:
const n: number = Observable.bindNodeCallback(fs.readFile);
will effect this error:
Type '(v1: string) => Observable' is not assignable to type 'number'.
So it's obvious that TypeScript is matching the path-only overload of readFile
.
In situations like this, I often use an arrow function to specify exactly which overload I want to use. For example, this:
const n: number = Observable.bindNodeCallback((
path: string,
encoding: string,
callback: (error: Error, buffer: Buffer) => void
) => fs.readFile(path, encoding, callback));
will effect this error:
Type '(v1: string, v2: string) => Observable' is not assignable to type 'number'.
So it's now matching the desired overload and the following will work:
let readFileAsObservable = Observable.bindNodeCallback((
path: string,
encoding: string,
callback: (error: Error, buffer: Buffer) => void
) => fs.readFile(path, encoding, callback));
let result = readFileAsObservable('./package.json', 'utf8');
result.subscribe(
buffer => console.log(buffer.toString()),
error => console.error(error)
);