[英]When using dynamic import in a function, how can I specify type info in global variable?
My simplified server code looks like below.我的简化服务器代码如下所示。
server.ts服务器.ts
import google from "googleapis";
const androidPublisher = google.androidpublisher("v3");
app.use('something', function(req, res, n){
...
})
...(only one of the dozens of other methods use androidPublisher)
I am importing googleapis
library in order to setup androidpublisher
variable.我正在导入
googleapis
库以设置androidpublisher
变量。 However, this googleapis
library is big and it takes 400ms~700ms to fully import file, when it takes 10ms~30ms to import other library files.但是,这个
googleapis
库很大,完全导入文件需要400ms~700ms,而导入其他库文件需要10ms~30ms。
Because my environment is serverless architecture (firebase functions), AND because approximately 1 out of 100 requests actually need androidPublisher
, I want to take advantage of dynamic import to import googleapis
when it is necessary.因为我的环境是无服务器架构(firebase 函数),并且因为大约 100 个请求中有 1 个实际上需要
androidPublisher
,所以我想在必要时利用动态导入来导入googleapis
。 Otherwise, above setup actually adds 400ms/700ms latency to every request that spins up new serverless instance, even when androidPublisher
is not needed.否则,即使
androidPublisher
,上述设置实际上也会为启动新的无服务器实例的每个请求增加 400 毫秒/700 毫秒的延迟。
So I have made changes like below.所以我做了如下改变。
server.ts服务器.ts
let androidPublisherInstance:any;
async function getAndroidPublisher() {
const googleapis = await import("googleapis");
if (androidPublisherInstance === undefined) {
const ap = googleapis.google.androidpublisher("v3");
androidPublisherInstance = ap;
}
return androidPublisherInstance;
}
...(one of methods use getAndroidPublisher() to get androidPublisher instance)
with above setup where I am using global variable & helper function to initialize androidPublisher only when needed.在上面的设置中,我仅在需要时使用全局变量和辅助函数来初始化 androidPublisher。 This works as intended and 400ms~700ms latency gets added when androidPublisher is needed for the first time.
这按预期工作,并且在第一次需要 androidPublisher 时会添加 400 毫秒~700 毫秒的延迟。 However, I ended up with type of
androidPublisherInstance
to be any
.但是,我最终将
androidPublisherInstance
类型设为any
。 I couldn't correctly define the type because type definition is available inside of googleapis
and that is residing inside of getAndroidPublisher
function.我无法正确定义类型,因为类型定义在
googleapis
内部可用,并且驻留在getAndroidPublisher
函数内部。
Thus, I lose all benefit of using typescript and have to play guess games while using methods/properties when I use androidPublisherInstance
.因此,当我使用
androidPublisherInstance
时,我失去了使用androidPublisherInstance
所有好处,并且必须在使用方法/属性的同时玩猜谜游戏。
And I think I must use global variable, because I do not want to initialize androidPublisher multiple times ( googleapis.google.androidpublisher("v3")
) whenever a function calls getAndroidPublisher()
而且我认为我必须使用全局变量,因为我不想在函数调用
getAndroidPublisher()
时多次初始化 androidPublisher ( googleapis.google.androidpublisher("v3")
getAndroidPublisher()
Am I missing something?我错过了什么吗? Is there a way to use dynamic import & let client to be initialized only once without needing to use global variable?
有没有办法使用动态导入并让客户端只初始化一次而无需使用全局变量?
You can just import the type.您可以只导入类型。 As long as you use it only in type definitions, not in value expressions, the compiled JavaScript will never load the module:
只要你只在类型定义中使用它,而不是在值表达式中使用它,编译后的 JavaScript 就永远不会加载模块:
import { androidpublisher_v3 } from "googleapis";
let androidpublisher: androidpublisher_v3 | undefined;
Alternatively, to make sure you don't accidentally reference it in the wrong place, use only import types :或者,为了确保您不会意外在错误的地方引用它,请仅使用导入类型:
let androidpublisher: import("googleapis").androidpublisher_v3 | undefined;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.