简体   繁体   English

Typescript - 类型“未知”上不存在属性“用户 ID”

[英]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.

相关问题 TypeScript中的类型不存在属性 - Property does not exist on type in TypeScript 打字稿-类型“ {..}”上不存在该属性 - Typescript - property does not exist on type '{..}' “未知”类型不存在属性“类型” - Property 'type' does not exist on type 'unknown' 类型“ AngularFireList”上不存在属性“ do” <unknown> “ - Property 'do' does not exist on type 'AngularFireList<unknown>' 类型“AngularFirestoreDocument”上不存在属性“withConverter”<unknown> - Property 'withConverter' does not exist on type 'AngularFirestoreDocument<unknown> 为什么 TypeScript 会给出错误消息 Property 'value' does not exist on type 'unknown'? - Why TypeScript gives the error message Property 'value' does not exist on type 'unknown'? Map 和 FlatMap 抛出错误“属性 &#39;map&#39; 在 Typescript 中的类型 &#39;unknown&#39; 上不存在 - Map and FlatMap throwing error "Property 'map' does not exist on type 'unknown' in Typescript TypeScript-类型“节点”上不存在属性“ id” - TypeScript - Property 'id' does not exist on type 'Node' Typescript 中的 Object 类型不存在属性 - Property does not exist on type Object in Typescript Typescript:“ Compositions”类型上不存在该属性 - Typescript: property does not exist on type 'Compositions'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM