[英]Typescript - Property 'userId' does not exist on type 'unknown'
I have the following simple code for practicing rxJS in Typescript.我有以下简单的代码用于在 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();
Why does the command prompt throws an error?为什么命令提示符会抛出错误?
C:\Users\Administrator\Downloads\typescript>ts-node from.ts
C:\Users\Administrator\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:423
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
from.ts:12:22 - error TS2339: Property 'userId' does not exist on type 'unknown'
.
12 .pipe(map(v => v.userId))
~~~~~~
...
i thought the code should work because the userId is already defined in return from<Promise<{userId: number}>>{...
我认为代码应该可以工作,因为 userId 已经定义为return from<Promise<{userId: number}>>{...
VScode also gives me the error message related with the userId Property 'userId' does not exist on type 'unknown'.ts(2339)
VScode 还给了我与userId Property 'userId' does not exist on type 'unknown'.ts(2339)
相关的错误消息
Please try below.请在下面尝试。
Replace代替
v.userId
To至
v['userId']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.