为什么 VSCode 给我这个订阅的类型错误?

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

我正在使用 VSCode 学习本教程 https://aws.github.io/aws-amplify/media/api_guide#subscriptions

我的 VSCode 一直告诉我 subscribe 方法有错误。

当我查看 graphql 方法的定义时,它告诉我 graphql({query, variables}: GraphQLOptions): Promise<GraphQLResult> | Observable<object>;

它可以 return 一个 promise 或一个 Observable。好吧,我正在尝试订阅这个 observable。我究竟做错了什么?为什么 VSCode 一直告诉我那里有错误?

错误是:'subscribe' property does not exist in type 'Promise<GraphQLResult> | Observable<object>'.

我是否必须在我的 tsconfig.json 中配置一些东西?

编辑:添加 graphql 方法定义的屏幕。

打字稿错误是正确的:.subscribe() 方法不在类型签名 Promise<GraphQLResult> | Observable<object> 中。这是因为它不存在于 Promise.

的原型中

为了让打字再次工作,您需要 return 只能从您的 graphql() 函数中观察到,或者将其 return 值转换为 Observable<object>(可能导致 nullref ).您还可以使用 Observable.from() 包装调用代码,它将 Promiselike 作为参数来规范化签名。