Angular Error: Type 'Observable<number | undefined>' is not assignable to type 'Observable<number>'
Angular Error: Type 'Observable<number | undefined>' is not assignable to type 'Observable<number>'
我正在 Angular 中创建一个将文件上传到 firebase 存储的服务,但是最新版本的 typescript 要求声明更具体,所以它不允许我有一个类型号的变量初始化为未定义,我在尝试获取文件上传百分比时收到以下错误:
Type 'Observable<number | undefined>' is not assignable to type 'Observable'.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.ts(2322)**
这是我的文件项class:
export class FileItem {
public name: string;
public uploading: boolean = false;
public uploadPercent?: Observable<number>;
public downloadURL?: Observable<string>;
constructor(public file: File = file) {
this.name = file.name;
}
这是我不知道如何修改的代码行:
item.uploadPercent = task.percentageChanges()
完整的函数为:
uploadFiles(folderPath: string, files: FileItem[]) {
for (const item of files) {
item.uploading = true;
const filePath = this.generateFileName(folderPath, item.name);
const fileRef = this.storage.ref(filePath);
const task = this.storage.upload(filePath, item.file);
item.uploadPercent = task.percentageChanges()
task
.snapshotChanges()
.pipe(
finalize(() => {
item.downloadURL = fileRef.getDownloadURL();
item.uploading = false;
})
)
.subscribe();
}
.percentageChanges() 方法returns 如下:
percentageChanges(): observable<number | undefined>;
我假设错误来自这一行(如果不是,请添加错误告诉您的确切代码部分(它告诉您数字行)。
那么...它允许你这样声明 uploadPercent?:
public uploadPercent?: Observable<number|undefined>;
如果是这样,我想通过这个小小的改变,您将解决错误。
如果没有,请尝试:
public uploadPercent?: Observable<number|any>;
如果这些解决方案中的 none 有效,请显示失败的确切代码部分,并且当 returns 'Observable<number | undefined>' 强制执行 'Observable<number | undefined>' 时,解决方案将通过更改=27=] 'Observable< number>'
我正在 Angular 中创建一个将文件上传到 firebase 存储的服务,但是最新版本的 typescript 要求声明更具体,所以它不允许我有一个类型号的变量初始化为未定义,我在尝试获取文件上传百分比时收到以下错误:
Type 'Observable<number | undefined>' is not assignable to type 'Observable'. Type 'number | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'.ts(2322)**
这是我的文件项class:
export class FileItem {
public name: string;
public uploading: boolean = false;
public uploadPercent?: Observable<number>;
public downloadURL?: Observable<string>;
constructor(public file: File = file) {
this.name = file.name;
}
这是我不知道如何修改的代码行:
item.uploadPercent = task.percentageChanges()
完整的函数为:
uploadFiles(folderPath: string, files: FileItem[]) {
for (const item of files) {
item.uploading = true;
const filePath = this.generateFileName(folderPath, item.name);
const fileRef = this.storage.ref(filePath);
const task = this.storage.upload(filePath, item.file);
item.uploadPercent = task.percentageChanges()
task
.snapshotChanges()
.pipe(
finalize(() => {
item.downloadURL = fileRef.getDownloadURL();
item.uploading = false;
})
)
.subscribe();
}
.percentageChanges() 方法returns 如下:
percentageChanges(): observable<number | undefined>;
我假设错误来自这一行(如果不是,请添加错误告诉您的确切代码部分(它告诉您数字行)。
那么...它允许你这样声明 uploadPercent?:
public uploadPercent?: Observable<number|undefined>;
如果是这样,我想通过这个小小的改变,您将解决错误。
如果没有,请尝试:
public uploadPercent?: Observable<number|any>;
如果这些解决方案中的 none 有效,请显示失败的确切代码部分,并且当 returns 'Observable<number | undefined>' 强制执行 'Observable<number | undefined>' 时,解决方案将通过更改=27=] 'Observable< number>'