最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to dynamically import named exports TypeScript - Stack Overflow

programmeradmin2浏览0评论

const { SchemaLink } = require('apollo-link-schema')

I've tried const { SchemaLink } = import('apollo-link-schema'), but it doesn't work. ESLint still shows an error @typescript-eslint/explicit-function-return-type

How can i dynamically import a named export in TypeScript?

const { SchemaLink } = require('apollo-link-schema')

I've tried const { SchemaLink } = import('apollo-link-schema'), but it doesn't work. ESLint still shows an error @typescript-eslint/explicit-function-return-type

How can i dynamically import a named export in TypeScript?

Share Improve this question edited Apr 26, 2023 at 0:51 Aluan Haddad 31.9k10 gold badges83 silver badges95 bronze badges asked May 14, 2020 at 19:48 Alexander KimAlexander Kim 18.4k24 gold badges107 silver badges165 bronze badges 2
  • 3 It would be const { SchemaLink } = await import('apollo-link-schema'). Also, linter errors are not the same as piler errors. If you disagree with the linter, perhaps that rule is not for you. – Aluan Haddad Commented May 15, 2020 at 4:15
  • @AluanHaddad, thanks. Since it's a promise based import, could you show an example with a Promise instead of await/async? – Alexander Kim Commented May 15, 2020 at 7:21
Add a ment  | 

1 Answer 1

Reset to default 7

Unlike require, JavaScript's dynamic import is asynchronous, returning a Promise for the specified module. Therefore, the module and its exports are only available after the Promise has successfully resolved. We can use the standard Promise API directly

import('apollo-link-schema').then(({ SchemaLink }) => {
  // Use SchemaLink here
});

Additionally, By taking advantage of async/await, we can write

const { SchemaLink } = await import('apollo-link-schema');
// Use SchemaLink here
发布评论

评论列表(0)

  1. 暂无评论