类型“{}”不可分配给类型 'string'

Type '{}' is not assignable to type 'string'

我正在使用 ngrx/router

当我打开 http://localhost/set-new-password/abc 时,RouteParams 运行良好。我可以得到我想要的字符串token

const landingRouter: Routes = [
  { path: '/set-new-password/:token', component: SetNewPasswordComponent },
];

export class SetNewPasswordComponent implements OnInit {
  token: string = '';

  constructor(private _routeParams$: RouteParams) {}

  ngOnInit()
  {
    this._routeParams$
      .pluck('token')
      .subscribe(token => {
        console.log(typeof token); // Console: "string"
        console.log(token);        // Console: "abc"
        this.token = token;        // Terminal: Type '{}' is not assignable to type 'string'.
      });
  }
}

但是,我在终端中收到此警告:

Type '{}' is not assignable to type 'string'.

我知道我可以使用 this.token = String(token); 来摆脱它。

但是为什么会出现这个警告呢?有人可以为我解释一下吗?谢谢

从@brandonroberts 获得@MikeRyan52 的原始答案的帮助,谢谢!

Two reasons why this won't work:

  1. We don't actually know the shape of the params object until runtime, so there's not a good type to describe it

  2. pluck('id') can't be well typed anyways since the string selector isn't known

Solution would be to explicitly type pluck():

params$.pluck<string>('id')

所以我的情况是:

this._routeParams$
  .pluck<string>('token')
  .subscribe(token => this.token = token);