【发布时间】:2022-01-22 07:55:32
【问题描述】:
使用 Google Auth2 API - @types/gapi.auth2 的类型时会出现错误。如果我创建一个使用 gapi.auth2.GoogleAuth 类型解析的承诺,编译器会抛出错误 1062。
Type is referenced directly or indirectly in the fulfillment callback of its own 'then' method.
打字有这个小怪癖:
class GoogleAuth {
...
/**
* Calls the onInit function when the GoogleAuth object is fully initialized, or calls the onFailure function if
* initialization fails.
*/
then(onInit: (googleAuth: GoogleAuth) => any, onFailure?: (reason: {error: string, details: string}) => any): any;
...
}
使用的代码是这样的:
async function getGapi() {
return new Promise<gapi.auth2.GoogleAuth>(resolve => {
...
});
}
promise 范围内的任何内容都无关紧要,只要它具有 GoogleAuth 类型 - 它就会不高兴。
问题肯定与类型有关,创建包装器或完全忽略错误可能很容易。 GoogleAuth 对象是“thennable”,但为什么会导致任何问题?有没有循环引用什么的?
更令人不安的是1062错误很少。我还没有求助于阅读编译器代码,但到目前为止我无法弄清楚它试图告诉我什么。
【问题讨论】:
-
我在ember-typings repo 中发现
ember-data分型报告的类似问题。他们选择修改他们的打字。 -
那么这是否表明 Typescript 本身存在问题或 auth2 类型存在问题?它作为普通的 Javascript 代码工作得非常好,所以我建议这是一个 Typescript 问题。
-
有趣。当你说它在javascript中工作时,这是否意味着
onInit回调在GoogleAuth.then()解析时接收到实际上具有then()方法的googleAuth对象?这会很奇怪,因为 promise 保证会解析为不是 promise 的东西。 -
不,你是对的,我已经假设了一些东西。这表明我们根本不能在 Promise 中使用 GoogleAuth 对象——即使在 javascript 中也是如此。 developers.google.com/identity/sign-in/web/…
标签: typescript