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
1 Answer
Reset to default 7Unlike 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