首页 > 解决方案 > 可观察回报与一般差异

问题描述

这样做是否被认为是不好的做法?

我最初说 observable 是 UserCreationResponse 类型,但随后使用 map 返回一个User. 这对我的用例来说很方便,因为 json 响应会自动映射到 UserCreationResponse,然后当我订阅 observable 时,我会得到用户。但我想知道它是否会误导阅读代码的人?

  signup(email : string, password : string) {
    return this.http.post<UserCreationResponse>("https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key="+this.apiKey, {
      email: email,
      password : password,
      returnSecureToken : true
    }).pipe(catchError(error => {return throwError("pipedErrror"+error.toString())}),
      map(resData => {
        const expirationDate = new Date(new Date().getTime() + +resData.expiresIn * 1000);
        const user = new User(
          resData.email, +resData.localId, resData.idToken, expirationDate

        )
        this.user.next(user);
        return user;
      }));

标签: typescript

解决方案


推荐阅读