For my rxJS practice in Typescript, I wrote the following simple code.
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 am I getting an error in the command prompt?
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 was expecting the code to work since the userId is defined in
return from<Promise<{userId: number}>>{...
VScode also gives me the error message regarding the userId
Property 'userId' does not exist on type 'unknown'.ts(2339)