Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Angular Typescript-Property';用户ID';不存在于类型';未知';_Angular_Typescript_Rxjs - Fatal编程技术网

Angular Typescript-Property';用户ID';不存在于类型';未知';

Angular Typescript-Property';用户ID';不存在于类型';未知';,angular,typescript,rxjs,Angular,Typescript,Rxjs,我有以下简单的代码用于在Typescript中练习rxJS import fetch from 'node-fetch'; import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent, SubscriptionLike, PartialObserver } from 'rxjs'; import { map, filter, scan } from 'rxjs/op

我有以下简单的代码用于在Typescript中练习rxJS

import fetch from 'node-fetch';
import {
    Observable, Subject, asapScheduler, pipe, of, from,
    interval, merge, fromEvent, SubscriptionLike, PartialObserver
} from 'rxjs';
import { map, filter, scan } from 'rxjs/operators';

function getUserId() {
    return from<Promise<{userId: number}>>(
        fetch('https://jsonplaceholder.typicode.com/posts/1').then(r => r.json())
    )
    .pipe(map(v => v.userId))
    .subscribe(console.log);
}

getUserId();
我认为代码应该可以工作,因为userId已经在
returnfrom{…

VScode还向我提供了与用户ID相关的错误消息
属性“userId”在类型“unknown”上不存在。ts(2339)

请在下面尝试

替换

v.userId


谢谢你的建议Raj。只要检查代码是否正常工作。我可以再问一个问题吗。v.userId和v['userId']之间有什么区别?@Daino我不知道这背后的事实,但我已经面对过很多次了。@Daino没有区别,
v['userId']
只是没有进行类型检查。因此,这更像是一种解决办法。
v.userId
v['userId']