我希望我的方法的返回类型作为给定接口的 Observable。但事实证明,API 上的资源可能为空(未找到)。所以,我想要的是强制开发人员(包括我自己)在订阅中使用资源之前检查资源是否为空。这可能吗?
像这样:
@Injectable({ providedIn: 'root' })
export class CustomerService extends BaseService {
getCustomer(): Observable<ICustomer | null> {
return null;
}
}
和电话:
this.customerApi.getCustomer().subscribe((something) => {
// should eslint an error here, because something.id might not exist
console.log('post', something.id);
});
这可能吗?