简体   繁体   English

在函数中使用动态导入时,如何在全局变量中指定类型信息?

[英]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.

相关问题 将 object 传递给 function 时,如何指定 TypeScript 类型? - How can I specify a TypeScript type when passing an object into a function? 当我使用JavaScript在表单中输入错误的类型信息时,如何停止表单提交? - How can I stop the form submit when I input wrong type info in the form using javascript? 动态导入开启。(“点击”,...)找不到全局变量 - dynamic import's on.(“click”, …) can't find global variable 如何将全局变量导入另一个文件? - How can import a global variable into another file? 如何从变量或道具导入组件 - 动态路由器 - How can I import component from variable or props - Dynamic router 如何在不使用全局变量的情况下使此函数工作 - How can I make this function work without using a global variable on react 如何使用 Parcel Bundler 在函数内设置全局变量? - How can I set a global variable inside a function using the Parcel Bundler? 为什么我在使用 Nuxt3 时不能使用动态导入 - Why can I not use a dynamic import when using Nuxt3 如何使此函数中包含的变量成为全局变量? - How can I make my variable contained within this function global? 调用函数后如何重置全局变量? - How can I reset the global variable after calling the function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM