gpt4 book ai didi

typescript - 类型 '{}' 不可分配给类型 'string'

转载 作者:搜寻专家 更新时间:2023-10-30 21:20:20 25 4
gpt4 key购买 nike

我正在使用 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);

关于typescript - 类型 '{}' 不可分配给类型 'string',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37633676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com