Javascript 无法将arrow函数的参数强制转换为接口?
我的代码如下所示:Javascript 无法将arrow函数的参数强制转换为接口?,javascript,angular,function,typescript,casting,Javascript,Angular,Function,Typescript,Casting,我的代码如下所示: interface ImyInterface { v: number; } class A implements OnInit { ngOnInit() { let myObservable$ = getObs(); myObservable$.subscribe(data => { const foo = data as ImyInterface; foo. // <-- V
interface ImyInterface {
v: number;
}
class A implements OnInit {
ngOnInit() {
let myObservable$ = getObs();
myObservable$.subscribe(data => {
const foo = data as ImyInterface;
foo. // <-- VS Code IDE autcompletes with 'v'
});
}
};
接口ImyInterface{
五:数量;
}
类实现了OnInit{
恩戈尼尼特(){
设myObservable$=getObs();
myObservable$.subscribe(数据=>{
const foo=作为imy接口的数据;
foo.//{
数据=作为IMY接口的数据;
数据。//您应该尝试这样做:
myObservable$.subscribe((data: ImyInterface) => {
alert(data.v);
});
使用上面的代码,您声明您的subscribe函数期望一个正在实现ImyInterface
接口的参数。很好。但是为什么我的方法失败了?感谢upvote=)
myObservable$.subscribe((data: ImyInterface) => {
alert(data.v);
});