Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法将arrow函数的参数强制转换为接口?_Javascript_Angular_Function_Typescript_Casting - Fatal编程技术网

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);
});