'The type "Observable<unknown>" cannot be assigned to the type "Observable<{token:string; }>". tap Error

I'm trying to add a token via the .pipe method

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { User } from './interfaces';

@Injectable()
export class AuthService {

  private token = null

  constructor(private http: HttpClient) {
  }

  login(user: User): Observable<{token: string}> {
    return this.http.post<{token: string}>('/api/auth/login', user)
     .pipe(tap(({token}) => {
        localStorage.setItem('auth-token', token)
        this.setToken(token)
     }))
  }

  setToken(token: string) {
    this.token = token
  }
}

interfaces.ts

export interface User {
  email: string,
  password: string
}

Everything works with this code

  login(user: User): Observable<{token: string}> {
    return this.http.post<{token: string}>('/api/auth/login', user)
  }

When I try to add a token via the .pipe method tap i have a Error

The type "Observable<unknown>" cannot be assigned to the type "Observable<{token:string; }>".
The "token" property is missing in the "{}" type and is mandatory in the "{token: string; }" type.ts(2322)

Any added method in the .pipe (tap, map and others) as soon as I import them from 'rxjs/operators', this error appears.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source