Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'token' does not exist on type 'Object'. Typescript issue

So I'm getting a compiler error stating that token doesn't exist on type Object. This object is getting returned from the server with two objects. A token and a user object. I'm not sure how to define res in the code example below so that typescript is okay with this.

return this.http.post(api.url, params)
        .map(res => {
          if (res && res.token) {
              localStorage.setItem('token', res.token);
              localStorage.setItem('user', JSON.stringify(res.user));
              return true;
          }

          return false;
        });
like image 381
bstory Avatar asked Jun 22 '26 10:06

bstory


1 Answers

Typescript is complaining the response does not have a property named token, Change it as,

return this.http.post(api.url, params)
        .map((res:any) => {
like image 159
Sajeetharan Avatar answered Jun 24 '26 19:06

Sajeetharan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!