首页 > 解决方案 > 是否让变量:typeof import('large-module'); 将整个模块导入内存?

问题描述

对于以下代码段:

let variable: typeof import('large-module');

async function expensiveOperation () {
  variable = await import('large-module');
  // ...do stuff
}

在调用函数之前是否将“大模块”加载到内存中?
(因为导入是写在类型定义中的)

标签: typescript

解决方案


它不是。打字稿将静态解析打字。您可以在编译的 javascript 中看到它:

let variable;
async function expensiveOperation() {
    variable = await import('large-module');
}

推荐阅读