Promise with Angular is not assignable to type ts2322.

huangapple go评论69阅读模式
英文:

Promise with Angular is not assignable to type ts2322

问题

I'm trying to do a login in Angular, but I get the error with the promises.

login(user?: string, password?: string): Promise<ApiResponse<User>> {
  return new Promise((resolve) => {
    let promise: Promise<ApiResponse<UserLogged>>;
    if (user && password) // I'm logging in
      promise = this.http.post<ApiResponse<UserLogged>>(this.baseUrl + "login", { user: user, password: password }).toPromise();
    else // Check if I already have an active session on the server
      promise = this.http.get<ApiResponse<UserLogged>>(this.baseUrl + "login").toPromise();
    // Process the response
  });

}

apiresponse.ts

export interface ApiResponse<T> extends BaseDTO {
  status: number;
  message?: string;
  data?: T
}

baseDTO

export interface BaseDTO {
}

Can someone explain to me what I am doing wrong.

Sorry, my English is poor.

Thank you.

User

export interface User extends BaseIdDTO {
  user: string;
  password?: string;
  mail: string;
  name?: string;
}

Change in tsconfig.json the "strict: true" to "strict: false" and the error disappears.

英文:

I'm trying to do a login in Angular, but I get the error with the promises.
"Type 'Promise&lt;ApiResponse&lt;UserLogged&gt; | undefined&gt;' is not assignable to type 'Promise&lt;ApiResponse&lt;UserLogged&gt;&gt;'. ts2322

     login(user?:string, password?:string):Promise&lt;ApiResponse&lt;User&gt;&gt; {
      return new Promise((resolve) =&gt;  {
        let promise:Promise&lt;ApiResponse&lt;UserLogged&gt;&gt;;
        if (user &amp;&amp; password)                           //me estoy logueando
          promise=this.http.post&lt;ApiResponse&lt;UserLogged&gt;&gt;(this.baseUrl + &quot;login&quot;, {user: user, password: password}).toPromise();
        else                                            //valido si ya tengo sesion activa en el server
          promise=this.http.get&lt;ApiResponse&lt;UserLogged&gt;&gt;(this.baseUrl + &quot;login&quot;).toPromise();
        //proceso respuesta
  
      });
      
    }

apiresponse.ts

export interface ApiResponse&lt;T&gt; extends BaseDTO{
  status: number;
  message?: string;
  data?: T
}

baseDTO

export interface BaseDTO { 
}

Can someone explain to me what I am doing wrong.

Sorry my English is poor

thank you

User

export interface User extends BaseIdDTO{
  user:string;
  password?:string;
  mail:string;
  name?:string

Change in tsconfig.json the "strict:true" to "strict: false" and the error disappears.

答案1

得分: 1

好的,我感谢你的建议,我是一个初学者,我会学习如何使用observable来做这件事。

英文:

ok, I thank you for your suggestions, I am a beginner, I will study how to do it with observable

答案2

得分: 0

开始时编写干净的代码?

login(user?: string, password?: string) {
  if (user && password) return this.http.post(this.baseUrl + 'login', { user, password }).toPromise();
  else return this.http.post(this.baseUrl + 'login').toPromise();
}

另外,你真的应该使用 observables,使用 promises 是一种降级。

英文:

Make clean code to start with ?

login(user?: string, password?: string) {
  if (user &amp;&amp; password) return this.http.post(this.baseUrl + &#39;login&#39;, { user, password }).toPromise();
  else return this.http.post(this.baseUrl + &#39;login&#39;).toPromise();
}

Also, you should really use observables, using promises is a downgrade.

huangapple
  • 本文由 发表于 2023年4月6日 20:09:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定