Search code examples
visual-studio-coderxjstypescript-typingssubscriptionaws-amplify

Why does VSCode give me a type error on this subscription?


I'm following this tutorial https://aws.github.io/aws-amplify/media/api_guide#subscriptions with my VSCode.

My VSCode keeps telling me there is an error on the subscribe method.

When I look at the definition of the graphql method, it tells me graphql({query, variables}: GraphQLOptions): Promise<GraphQLResult> | Observable<object>;

It can return a promise or an Observable. Well I'm trying to subscribe to this observable. What am I doing wrong? Why does VSCode keep telling me there is an error there?

enter image description here

The error is: 'subscribe' property does not exist in type 'Promise<GraphQLResult> | Observable<object>'.

Do I have to configure something in my tsconfig.json?

EDIT: Add a screen of the definition of graphql method.

enter image description here


Solution

  • The typescript error is correct: the .subscribe() method is not present in the type signature Promise<GraphQLResult> | Observable<object>. This is because it is not present in the prototype of Promise.

    To get the typings to work again you either need to return only observable from your graphql() function or cast its return value to Observable<object> (possible causing nullref). You can also wrap the calling code with Observable.from() which takes Promiselike as argument to normalize the signatures.