无法在 TypeScript 中从 Observable.bindNodeCallback(fs.readFile) 创建 observable
Cannot create observable from Observable.bindNodeCallback(fs.readFile) in TypeScript
我正在尝试使用 rxjs 5 在 TypeScript 中编写一个 Node.js 服务器,但是在将 fs.readFile
转换为它的 rxjs 形式时我遇到了错误。我希望以下代码可以在 TypeScript
中运行
// This is a JavaScript example from the official documentation. It should
// also work at the TypeScript envrionment.
import * as fs from 'fs';
import { Observable } from 'rxjs';
let readFileAsObservable = Observable.bindNodeCallback(fs.readFile);
// This is the line that throws the error.
let result = readFileAsObservable('./roadNames.txt', 'utf8');
result.subscribe(x => console.log(x), e => console.error(e));
但是,当我添加第二个参数时,我的编辑器报告 TypeScript 错误 'utf-8'
Supplied parameters do not match any signature of call target.
我试图找到有关如何在 rxjs 和 TypeScript 中使用 fs.readFile()
的指南,但运气不佳。
bindCallback
和 bindNodeCallback
对于 TypeScript 来说可能很棘手,因为这完全取决于 TypeScript 如何推断函数参数。
可能有更好的方法,但这是我为准确了解所推断的内容所做的工作:将可观察对象分配给完全不兼容的对象,并仔细查看受影响的错误。例如,这个:
const n: number = Observable.bindNodeCallback(fs.readFile);
将影响此错误:
Type '(v1: string) => Observable<Buffer>' is not assignable to type 'number'.
所以很明显 TypeScript 匹配 readFile
.
的仅路径重载
在这种情况下,我经常使用箭头函数来准确指定我想使用的重载。例如,这个:
const n: number = Observable.bindNodeCallback((
path: string,
encoding: string,
callback: (error: Error, buffer: Buffer) => void
) => fs.readFile(path, encoding, callback));
将影响此错误:
Type '(v1: string, v2: string) => Observable<Buffer>' is not assignable to type 'number'.
所以它现在匹配所需的重载,以下将起作用:
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)
);
老实说我还没有找到解决方案,但为了让它工作,我将它转换为一个函数。
(<Function>Rx.Observable.bindNodeCallback(fs.readFile))('./file.txt', 'utf8').subscribe();
我正在尝试使用 rxjs 5 在 TypeScript 中编写一个 Node.js 服务器,但是在将 fs.readFile
转换为它的 rxjs 形式时我遇到了错误。我希望以下代码可以在 TypeScript
// This is a JavaScript example from the official documentation. It should
// also work at the TypeScript envrionment.
import * as fs from 'fs';
import { Observable } from 'rxjs';
let readFileAsObservable = Observable.bindNodeCallback(fs.readFile);
// This is the line that throws the error.
let result = readFileAsObservable('./roadNames.txt', 'utf8');
result.subscribe(x => console.log(x), e => console.error(e));
但是,当我添加第二个参数时,我的编辑器报告 TypeScript 错误 'utf-8'
Supplied parameters do not match any signature of call target.
我试图找到有关如何在 rxjs 和 TypeScript 中使用 fs.readFile()
的指南,但运气不佳。
bindCallback
和 bindNodeCallback
对于 TypeScript 来说可能很棘手,因为这完全取决于 TypeScript 如何推断函数参数。
可能有更好的方法,但这是我为准确了解所推断的内容所做的工作:将可观察对象分配给完全不兼容的对象,并仔细查看受影响的错误。例如,这个:
const n: number = Observable.bindNodeCallback(fs.readFile);
将影响此错误:
Type '(v1: string) => Observable<Buffer>' is not assignable to type 'number'.
所以很明显 TypeScript 匹配 readFile
.
在这种情况下,我经常使用箭头函数来准确指定我想使用的重载。例如,这个:
const n: number = Observable.bindNodeCallback((
path: string,
encoding: string,
callback: (error: Error, buffer: Buffer) => void
) => fs.readFile(path, encoding, callback));
将影响此错误:
Type '(v1: string, v2: string) => Observable<Buffer>' is not assignable to type 'number'.
所以它现在匹配所需的重载,以下将起作用:
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)
);
老实说我还没有找到解决方案,但为了让它工作,我将它转换为一个函数。
(<Function>Rx.Observable.bindNodeCallback(fs.readFile))('./file.txt', 'utf8').subscribe();